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

查看:90
本文介绍了如何连接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没有commapd属性. 如果控件没有命令属性,那么如何结合ICommand和控件的事件?

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代码

<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天全站免登陆