I’ll start off with the code as it should be fairly self-explanatory:
<commonControls:SearchTextBox
x:Name="searchTextBox"
Margin="6,0"
HorizontalAlignment="Right"
MinWidth="50"
Width="130"
SearchMode="Instant"
>
<i:Interaction.Triggers>
<i:EventTrigger EventName="Search">
<cmd:EventToCommand Command="{Binding Search}"
CommandParameter="{Binding ElementName=searchTextBox, Path=Text}" />
</i:EventTrigger>
<i:EventTrigger EventName="Cancel">
<!-- Code to set searchTextBox.Text to Empty -->
</i:EventTrigger>
</i:Interaction.Triggers>
</commonControls:SearchTextBox>
The SearchTextBox has an Event called Cancel which executes if the user clicks the X button on the right hand side of the TextBox. What I would like to do is hook onto that event, and clear the Text property of the TextBox. I’m looking for a way to do this purely in XAML, with no Code Behind, and without hitting the ViewModel.
You can achieve this using a concept called ‘attached behaviours’, attached properties that on attachment handle events on the target object and perform some action as a result. See the following article:
http://www.codeproject.com/KB/WPF/AttachedBehaviors.aspx
I see you are already using blend interactions. Blend provides a simple framework for behaviours making them easier to implement, you simple override the
OnAttachmethod to add your logic. See the example here:http://www.scottlogic.co.uk/blog/colin/2011/06/metro-in-motion-part-7-panorama-prettiness-and-opacity/