获取所选TreeViewItem的位置

本文关键字:位置 TreeViewItem 获取 | 更新日期: 2023-09-27 18:24:43

我有一个带有以下XAML的TreeView

<TreeView ItemsSource="{Binding Folders, UpdateSourceTrigger=PropertyChanged}" x:Name="tree">
    <TreeView.InputBindings>
        <KeyBinding Key="Delete" 
                    Command="{Binding DataContext.DeleteFolderCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                    CommandParameter="{Binding ElementName=tree, Path=SelectedItem}"/>
        <KeyBinding Key="F2"
                    Command="{Binding DataContext.RenameFolderCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                    CommandParameter="{Binding ElementName=tree, Path=SelectedItem}"/>
    </TreeView.InputBindings>
    <TreeView.ItemTemplate>
        <HierarchicalDataTemplate ItemsSource="{Binding Folders, UpdateSourceTrigger=PropertyChanged}">
            <Grid>
                <Label Content="{Binding Name}">
                    <Label.ContextMenu>
                        <ContextMenu>
                            <MenuItem Header="Rename" 
                                      Command="{Binding DataContext.RenameFolderCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                                      CommandParameter="{Binding ElementName=tree, Path=SelectedItem}"/>
                            <MenuItem Header="Delete" 
                                      Command="{Binding DataContext.DeleteFolderCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                                      CommandParameter="{Binding ElementName=tree, Path=SelectedItem}"/>
                        </ContextMenu>
                    </Label.ContextMenu>
                    <Label.InputBindings>
                        <MouseBinding MouseAction="LeftDoubleClick"
                            Command="{Binding DataContext.SelectFolderCommand, RelativeSource={RelativeSource FindAncestor, AncestorType=UserControl}}"
                            CommandParameter="{Binding ElementName=tree, Path=SelectedItem}" />
                    </Label.InputBindings>
                </Label>
            </Grid>
        </HierarchicalDataTemplate>
    </TreeView.ItemTemplate>
</TreeView>

单击TreeViewItem上的"重命名"(或按F2),我想用TextBox替换标签。

最好的方法是什么?

我已经尝试获取TreeView的SelectedItem的PositionBounds,但那是Folder的一个实例,所以我无法获取那里的信息。

获取所选TreeViewItem的位置

我发现,实现所需功能的更好方法是永久使用TextBox,但其IsReadOnly属性设置为true。您可以将TextBox.IsReadOnly属性设置为True以使其可编辑,而不是将Label替换为TextBox

此外,您还可以添加Style Setter s,使TextBox在不可编辑时看起来不是TextBox

<Style x:Key="TextBlockStyle" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="IsReadOnly" Value="True">
            <Setter Property="Background" Value="{x:Null}" />
            <Setter Property="BorderBrush" Value="{x:Null}" />
            <Setter Property="BorderThickness" Value="0" />
        </Trigger>
    </Style.Triggers>
</Style>

然后,您可以从代码隐藏或视图模型中数据绑定bool IsEditable属性,以更改TextBox是否应可编辑:

<TextBox Text="{Binding SomeTextProperty}" IsReadOnly="{Binding IsEditable}" ... />

然后,要使字段可编辑,您需要做的就是(在代码隐藏或视图模型中):

IsEditable = true;

您通常会创建一个具有可编辑模式(如combobox)的UserControl,并更改模式运行时。出于类似的目的,我更改了combobox模板并构建了自己的控件。

顺便说一下,我建议看一下combobox控件模板。(是否可编辑)