简体   繁体   English

TreeViewItem DataTemplate LostFocus 事件中的文本框未触发

[英]TextBox in TreeViewItem DataTemplate LostFocus event not firing

I'm making an EditableTextBox by switching between two different DataTemplate to use it in TreeViewItem .我通过在两个不同的DataTemplate之间切换来制作一个EditableTextBox以在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 . Item 的ViewModel仅具有一个属性IsEditable ,用于在此处在两个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 .我认为TextBox LostFocus将成为 go 的方式,但此事件不会触发,因此EditableTextBox保持在编辑模式,除非我 select 另一个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?有没有办法获取任何类型的事件来帮助了解在TextBox外部发生了点击,从而将IsEditable属性更改为 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?" “有没有办法获取任何类型的事件,有助于知道在 TextBox 外部发生了点击,从而将 IsEditable 属性更改为 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.也许对你来说有点太多了,但你可以通过 EventArgs 过滤点击了什么样的控件等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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