0

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

lecloneur
  • 424
  • 5
  • 20
  • [This](https://stackoverflow.com/questions/6489032/wpf-remove-focus-when-clicking-outside-of-a-textbox) question can be useful – Insane Sep 24 '19 at 04:21
  • You could also use the `DataGrid` `SelectedIndexChanged` and `DataGrid` `LostFocus` events to globally handle the exiting of the edit mode. In that way you will have much less event handlers and less events firing. Also I think that the `TextBox` `LostFocus` doesnt fire but rather the `Cell` `FocusLost`, I am not sure about the later one thought. – Prophet Lamb Sep 24 '19 at 06:15
  • @lecloneur: Did you try the `LostKeyboardFocus` event? – mm8 Sep 24 '19 at 14:01
  • Exactly the same problem with `LostKeyboardFocus ` – lecloneur Sep 25 '19 at 11:05

1 Answers1

0

"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 ?"

Only regarding to this sentence, you could easily use

System.Windows.Input.MouseButtonEventHandler

Maybe a bit too much for you, but you could filter via the EventArgs what kind of control etc. was clicked.

Spevacus
  • 584
  • 2
  • 13
  • 23
Metzicious
  • 11
  • 3