简体   繁体   中英

TextBox in TreeViewItem DataTemplate LostFocus event not firing

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM