【发布时间】:2011-03-30 07:45:09
【问题描述】:
最近,我意识到 MVVM 模式对于 Silverlight 应用程序非常有用,并正在研究如何将它应用到我的项目中。
顺便说一句,如何用命令连接文本框的 textChanged 事件? Button 有 Command 属性,但 Textbox 没有 commapd 属性。 如果Controls没有command属性,如何结合ICommand和Control的事件?
我得到了以下 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 属性,例如文本框。
谢谢
【问题讨论】:
-
在研究了几个小时来解决这个问题后,我发现了 MvvmLightToolkit(mvvmlight.codeplex.com) 它使得 MVVM 的使用变得很容易。再次感谢大家。
-
这个 link 是 UpdateSourceTrigger=PropertyChanged 的更好解决方案
标签: xaml silverlight mvvm