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>
I'm not positive how to do it with
Interaction.Triggers
, but in the past I've used theAttachedCommand
dependency properties found here and simply used a StyleNormal XAML
Using a Style
EDIT: Since you can't download files at work, here's the code
CommandBehavior.cs
CommandBehaviorBinding.cs
EventHandlerGenerator.cs
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.