我有多个文本框绑定到了不同的数据,但是当TextChanged事件被触发时,我希望每个文本框都启动相同的命令.我可以在每个文本框下复制交互行,但我想必须有一种使用模板或样式的方法来使它们适用于所有文本框.
I have multiple textboxes bound to different data, but I would like every one to launch the same command when the TextChanged event is fired. I could copy the interaction line under every textbox but I'm guessing there must be a way to use a template or style to get this working on all of them.
这是第一个文本框的代码
Here is the code for the first textbox
<TextBox Grid.Row="2" Grid.Column="1" Grid.ColumnSpan="3" Height="28" HorizontalAlignment="Stretch" Margin="0,12,12,0" Name="TextBox_Description" VerticalAlignment="Top" TabIndex="3" Text="{Binding Item.Description, ValidatesOnDataErrors=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"> <i:Interaction.Triggers> <i:EventTrigger EventName="TextChanged"> <i:InvokeCommandAction Command="{Binding DataChangedCommand}"/> </i:EventTrigger> </i:Interaction.Triggers> </TextBox>推荐答案
如果您的UpdateMode始终为PropertyChanged,则确实没有任何理由不让您在ViewModel中监听您自己的PropertyChangedEvent.肯定会提高可测试性.
If your UpdateMode is always going to be PropertyChanged then there really isn't any reason not to listen to your own PropertyChangedEvent in your ViewModel. It will definitely improve testability.
private void OnPropertyChanged(object sender, PropertyChangedEventArgs args) { switch (args.PropertyName) { case "Prop1": case "Prop2": . . . DataChangedCommand.Execute(null); break; } }更多推荐
将相同的交互触发器应用于多个TextBox
发布评论