我有一个简单的 WM7 页面,带有TextBox
. 此外,我将EventToCommand
(a RelayCommand<string>
) 分配给 this ,对事件TextBox
做出反应。TextChanged
为了测试pourposes,我TextBox_TextChanged
在页面后面的代码中做了额外的方法。命令并TextBox_TextChanged
打印带有文本框内容的消息框。
TextBox
的初始值为"ABC"
。然后我按 D 并:
TextBox_TextChanged
打印ABCD
。- 命令打印
ABC
. D 不见了。
为什么命令这么快?
命令声明:
public RelayCommand<string> TextChanged {get; private set;}
命令初始化:
TextChanged = new RelayCommand<string>((s) => MessageBox.Show(s));
命令绑定:
<TextBox x:Name="SearchTextBox" Margin="10,0" TextWrapping="Wrap" Text="{Binding SearchString, Mode=TwoWay}" FontStyle="Italic" TextChanged="SearchTextBox_TextChanged" >
<i:Interaction.Triggers>
<i:EventTrigger EventName="TextChanged">
<GalaSoft_MvvmLight_Command:EventToCommand Command="{Binding TextChanged, Mode=OneWay}" CommandParameter="{Binding Text, ElementName=SearchTextBox}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>