将相同的交互触发器应用于多个TextBox

编程入门 行业动态 更新时间:2024-10-21 17:21:17
本文介绍了将相同的交互触发器应用于多个TextBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有多个文本框绑定到了不同的数据,但是当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

本文发布于:2023-10-28 01:09:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1535055.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:多个   触发器   应用于   TextBox

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!