我有多个文本框绑定(bind)到不同的数据,但我希望每个文本框在触发 TextChanged 事件时启动相同的命令。我可以复制每个文本框下的交互行,但我猜必须有一种方法可以使用模板或样式来使其对所有文本框都有效。
这是第一个文本框的代码
<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。它肯定会提高可测试性。
private void OnPropertyChanged(object sender, PropertyChangedEventArgs args)
{
switch (args.PropertyName)
{
case "Prop1":
case "Prop2":
.
.
.
DataChangedCommand.Execute(null);
break;
}
}
关于xaml - 将相同的交互触发器应用于多个文本框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6957301/