将相同的交互触发器应用于多个TextBox [英] Applying same interaction trigger to multiple TextBoxes

查看:91
本文介绍了将相同的交互触发器应用于多个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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆