如何连接 TextBox 的 TextChanged 事件和命令以便在 Silverlight 中使用 MVVM 模式 [英] How to hookup TextBox's TextChanged event and Command in order to use MVVM pattern in Silverlight

查看:22
本文介绍了如何连接 TextBox 的 TextChanged 事件和命令以便在 Silverlight 中使用 MVVM 模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最近,我意识到 MVVM 模式对于 Silverlight 应用程序非常有用,并正在研究如何将其应用到我的项目中.

Recently, I realized that MVVM pattern is so useful for Silverlight application and studying how to adopt it into my project.

顺便说一句,如何使用 Command 连接文本框的 textChanged 事件?Button 有 Command 属性,但是 Textbox 没有 COMAMPD 属性.如果 Controls 没有 command 属性,如何结合 ICommand 和 Control 的事件?

BTW, how to hook up the textbox's textChanged event with Command? There is Command property for Button, however Textbox has no commapd property. If Controls don't have command property, how to combine ICommand and Control's event?

我得到了以下 xaml 代码

I got following xaml code

<UserControl.Resources>
        <vm:CustomerViewModel x:Key="customerVM"/>    
    </UserControl.Resources>

    <Grid x:Name="LayoutRoot" 
          Background="White" 
          DataContext="{Binding Path=Customers, Source={StaticResource customerVM}, Mode=TwoWay}" >

        <StackPanel>
            <StackPanel Orientation="Horizontal"
                        Width="300"
                        HorizontalAlignment="Center">
                <TextBox x:Name="tbName" 
                         Width="50" 
                         Margin="10"/>
                <Button Width="30" 
                        Margin="10" 
                        Content="Find"
                        Command="{Binding Path=GetCustomersByNameCommand, Source={StaticResource customerVM}}"
                        CommandParameter="{Binding Path=Text, ElementName=tbName}"/>
            </StackPanel>
            <sdk:DataGrid ItemsSource="{Binding Path=DataContext, ElementName=LayoutRoot}"
                          AutoGenerateColumns="True"
                          Width="300"
                          Height="300"/>
        </StackPanel>
    </Grid>

我想要做的是,如果用户在文本框中输入一些文本,数据将显示在数据网格中,而不是使用按钮单击.我知道内置了自动完成框控件.但是,我想知道如何在没有 Command 属性的控件(如文本框)中调用 ViewModel 类中的 Command 属性.

What I am trying to do is that if user input some text in the textbox, data will be shown in the datagrid instead of using button click. I know there is autocomplete box control built in. however, I want to know how to call Command property in the ViewModel class in the controls which does not have Command property such as textbox.

谢谢

推荐答案

为什么不将 Text 属性绑定到视图模型上的属性?这样你会在更改时收到通知,并获得新值:

Why not just bind the Text property to a property on your view model? That way you get notified when it has changed, and also get the new value:

public string MyData
{
    get { return this.myData; }
    set
    {
        if (this.myData != value)
        {
            this.myData = value;
            this.OnPropertyChanged(() => this.MyData);
        }
    }
}

XAML:

<TextBox Text="{Binding MyData}"/>

这篇关于如何连接 TextBox 的 TextChanged 事件和命令以便在 Silverlight 中使用 MVVM 模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆