I have a window which contains a a TabControl with 2 TabItems inside it. Inside the second tab, there is a TextBox with a binding to a string property inside my view model. I want to be able to clear this property (or the TextBox since it is bound it will will by association, clear the property) whenever the user clicks on a different tab (and also if the user closes the window). I'm hoping I can achieve this with only the XAML since I am trying to follow the MVVM pattern (so no code behind and my view model shouldn't know of the TabControl or TextBox that is in my view).
I have tried a few different things and can't quite get it working and while I'm getting better at WPF, I'm still fairly new to it. Inside my tabs I also have ListViews and ComboBoxes, which from my understanding will bubble the SelectionChanged event (same as when the tab selection is changed) so I should probably stay away from that. Any help would be appreciated.
Here was one of my attempts (snippet showing the gist):
<TabControl>
<TabItem>
...Stuff in first tab goes here
</TabItem>
<TabItem Header="Rules">
<TabItem.Style>
<Style>
<Style.Triggers>
<DataTrigger Binding="{IsSelected}" Value="True">
<Setter TargetName="UserBox" Property="Text" Value="" />
</DataTrigger>
</Style.Triggers>
</Style>
</TabItem.Style>
<GroupBox>
<UniformGrid>
<ListView ItemsSource="{Binding Source={StaticResource rulesViewModel}, Path=RulesList}">
<ListView.View>
<GridView>
<GridViewColumn Header="Rules"/>
</GridView>
</ListView.View>
</ListView>
<DockPanel>
<TextBox Name="UserBox" Text="{Binding Source={StaticResource rulesViewModel}, Path=User}" />
</DockPanel>
</UniformGrid>
</GroupBox>
</TabItem>
I'm pretty sure the line that reads Binding="{IsSelected}" is wrong. But I'm not sure how to target the correct property. Any ideas?