简体   繁体   English

TreeViewItem 模板点击/选择/高亮问题

[英]TreeViewItem template click/select/highlight issue

I am new to WinUI 3 and I am currently building a TreeView (CommunityToolkit) where I can drag/drop TreeViewItems on top of each other.我是 WinUI 3 的新手,目前正在构建一个 TreeView (CommunityToolkit),我可以在其中拖放 TreeViewItems。 The TreeViewItem that I have consist of 3 parts, a group name, a display name and children items.我拥有的 TreeViewItem 由 3 个部分组成,一个组名称、一个显示名称和子项目。 The drag/drop part of the code works fine, however there is an issue whereby clicking on an item doesn't always select/highlight it and I cannot seem to find the root issue as to why.代码的拖放部分工作正常,但是存在一个问题,即单击某个项目并不总是选择/突出显示它,而且我似乎无法找到关于原因的根本问题。 See image below.见下图。

在此处输入图像描述

In the image above, the first item is "selected" as I would like it to be with the blue highlight to the left.在上图中,第一个项目被“选中”,因为我希望它在左侧有蓝色突出显示。 But when I click on either of the other 2 items (Level 1 or Level 2), I have observed the following behaviours.但是当我点击其他 2 个项目(1 级或 2 级)中的任何一个时,我观察到以下行为。

  1. A click on "U" or "Level 1" does not select the item.单击“U”或“1 级”不会 select 该项目。 There is some "pressed" style showing, but once the mouse button is released nothing happens.显示了一些“按下”样式,但是一旦释放鼠标按钮,什么也没有发生。 There is no highlight or selected style present没有突出显示或选择的样式
  2. A click just above or below the red line selects the item as I expect it to.单击红线上方或下方可以选择我期望的项目。

See the XAML below见下方XAML

<Grid>
    <Border BorderThickness="2" BorderBrush="DimGray">
        <TreeView AllowDrop = "True" 
                  CanDragItems="True"
                  CanReorderItems = "False"
                  ItemsSource="{x:Bind Items}"
                  SelectedItem="{x:Bind SelectedDemoItem, Mode=TwoWay}">
            <TreeView.ItemTemplate>
                <DataTemplate x:DataType="local:DemoItem">
                    <TreeViewItem AllowDrop="True" 
                                  CanDrag="True"
                                  CollapsedGlyph=""
                                  ExpandedGlyph=""
                                  IsExpanded="True"
                                  ItemsSource="{x:Bind Children}"
                                  Padding="-10,0,0,0">
                        <TreeViewItem.Content>
                            <StackPanel AllowDrop="True" 
                                        BorderBrush="Red"
                                        BorderThickness="1"
                                        CanDrag="True"
                                        Orientation="Horizontal">
                                <TextBlock FontSize="14" 
                                           FontWeight="ExtraBold"
                                           IsColorFontEnabled="True"
                                           Margin="0,0,10,0"
                                           MinWidth="30"
                                           TextAlignment="Center" 
                                           Text="{x:Bind Group}" />
                                <TextBlock Text="{x:Bind DisplayName}" Margin="0,0,5,0"/>
                            </StackPanel>
                        </TreeViewItem.Content>
                    </TreeViewItem>
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </Border>
</Grid>

And the code-behind和代码隐藏

public sealed partial class TestUserControl : UserControl
{
    public TestUserControl()
    {
        InitializeComponent();
        FillData();
    }

    private void FillData()
    {
        var level0 = new DemoItem { DisplayName = "Level 0", Group = Groups.M };
        var level1 = new DemoItem { DisplayName = "Level 1", Group = Groups.U };
        var level2 = new DemoItem { DisplayName = "Level 2", Group = Groups.C };

        level1.Children.Add(level2);
        level0.Children.Add(level1);

        Items.Add(level0);
        Items.Add(level0);
    }

    public ObservableCollection<DemoItem> Items { get; } = new();

    public DemoItem SelectedDemoItem { get; set; }

}

public enum Groups
{
    S, M, U, C
}

public class DemoItem
{
    public string DisplayName { get; set; }
    public ObservableCollection<DemoItem> Children { get; } = new();
    public Groups Group { get; set; }
}

For the purpose of this test, I have removed all drag/drop code as they have no affect on the problem above.出于此测试的目的,我删除了所有拖放代码,因为它们对上述问题没有影响。 However, it may help to mention that I have seen this problem occur only when CanDrag is set to True within my item template.但是,我发现只有在我的项目模板中将 CanDrag 设置为 True 时才会出现此问题,这可能会有所帮助。

Any help to fix this will be greatly appreciated.解决此问题的任何帮助将不胜感激。

Click events won't reach the TreeViewItem because of the TextBlocks .由于TextBlocks ,点击事件不会到达TreeViewItem The easiest way is to disable IsHistTestVisible on both TextBlocks .最简单的方法是在两个TextBlocks上禁用IsHistTestVisible

<TextBlock
    MinWidth="30"
    Margin="0,0,10,0"
    FontSize="14"
    FontWeight="ExtraBold"
    IsColorFontEnabled="True"
    IsHitTestVisible="False"
    Text="{x:Bind Group}"
    TextAlignment="Center" />
<TextBlock
    Margin="0,0,5,0"
    IsHitTestVisible="False"
    Text="{x:Bind DisplayName}" />

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

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