I'm making an EditableTextBox
by switching between two different DataTemplate
to use it in TreeViewItem
. Now entering in edit mode is working fine and I'm looking for a way to exit buy clicking anywhere.
The ViewModel
for the Item just have a property IsEditable
which is use to here to switch between the two DataTemplate
.
I thought TextBox
LostFocus
would be the way to go but this event is not firing therefore the EditableTextBox
stays in edit mode unless I select an other TreeViewItem
.
<DataTemplate x:Key="NormalTemplate">
<StackPanel Orientation="Horizontal">
<TextBlock Text="{Binding Path=Name}" Margin="3">
<i:Interaction.Triggers>
<i:EventTrigger EventName="PreviewMouseDown" >
<i:InvokeCommandAction Command="{Binding PreviewMouseDownCommand}" CommandParameter="{Binding}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBlock>
</StackPanel>
</DataTemplate>
<DataTemplate x:Key="EditTemplate">
<StackPanel>
<TextBox Text="{Binding Path=Name}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="LostFocus" >
<i:InvokeCommandAction Command="{Binding LostFocusCommand}" />
</i:EventTrigger>
</i:Interaction.Triggers>
</TextBox>
</StackPanel>
</DataTemplate>
<HierarchicalDataTemplate DataType="{x:Type ViewModels:DirectoryItem}" ItemsSource="{Binding Items}">
<ContentPresenter Content="{Binding}">
<ContentPresenter.Style>
<Style TargetType="{x:Type ContentPresenter}">
<Setter Property="ContentTemplate" Value="{StaticResource NormalTemplate}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding IsEditable}" Value="True">
<Setter Property="ContentTemplate" Value="{StaticResource EditTemplate}" />
</DataTrigger>
</Style.Triggers>
</Style>
</ContentPresenter.Style>
</ContentPresenter>
</HierarchicalDataTemplate>
<TreeView
ItemsSource="{Binding ResourceItems}">
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected}" />
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
Is there a way to get whatever kind of Event that would help to know that a click happened outside the TextBox
and therefore change the IsEditable
property to false ?
Thank you