XAML 我希望使用SelectionMode仅显示树视图中顶级项目的复选框

hl0ma9xz  于 2023-02-27  发布在  其他
关注(0)|答案(1)|浏览(114)

我将TreeViewSelectionMode设置为“Single”和“Multiple”。它不显示顶级复选框。此选项不可用吗?
请检查图像。

  • 树视图x:名称=“文件树”选择模式=“单个”
  • 树视图x:名称=“文件树”选择模式=“多个”

p5cysglq

p5cysglq1#

一种方法是“隐藏”CheckBox。您可以使用DataTemplateSelector定位项目。检查以下示例代码:

项目.cs

using System.Collections.Generic;

namespace TreeViewTest;

public interface IItem
{
    string Text { get; set; }
}

public class ParentItem : IItem
{
    public string Text { get; set; } = string.Empty;

    public List<IItem>? Children { get; set; }
}

public class ChildItem : IItem
{
    public string Text { get; set; } = string.Empty;
}

树视图数据模板选择器.cs

using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml;
using System;

namespace TreeViewTest;

public class TreeViewDataTemplateSelector : DataTemplateSelector
{
    public DataTemplate? ParentItemTemplate { get; set; }

    public DataTemplate? ChildItemTemplate { get; set; }

    protected override DataTemplate? SelectTemplateCore(object item)
    {
        return item switch
        {
            ParentItem => ParentItemTemplate,
            ChildItem => ChildItemTemplate,
            _ => throw new NotSupportedException(),
        };
    }
}

主页.xaml.cs

using Microsoft.UI.Xaml.Controls;
using System.Collections.Generic;

namespace TreeViewTest;

public sealed partial class MainPage : Page
{
    public MainPage()
    {
        this.InitializeComponent();

        Items.Add(new ParentItem()
        {
            Text = "Test tree1",
            Children = new List<IItem>()
            {
                new ChildItem() {Text = "Test tree1 child" },
                new ChildItem() {Text = "Test tree1 child2" },
            }
        });

        Items.Add(new ParentItem()
        {
            Text = "Test tree2",
            Children = new List<IItem>()
            {
                new ChildItem() {Text = "Test tree2 child" },
                new ChildItem() {Text = "Test tree2 child2" },
            }
        });

        Items.Add(new ParentItem()
        {
            Text = "Test tree3",
            Children = new List<IItem>()
            {
                new ChildItem() {Text = "Test tree3 child" },
                new ChildItem() {Text = "Test tree3 child2" },
            }
        });
    }

    public List<IItem> Items { get; set; } = new();
}

主页.xaml

<Page
    x:Class="TreeViewTest.MainPage"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:local="using:TreeViewTest"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    Background="{ThemeResource ApplicationPageBackgroundThemeBrush}"
    mc:Ignorable="d">

    <Page.Resources>
        <DataTemplate
            x:Key="ParentItemTemplate"
            x:DataType="local:ParentItem">
            <TreeViewItem
                Content="{x:Bind Text, Mode=OneWay}"
                ItemsSource="{x:Bind Children, Mode=OneWay}" />
        </DataTemplate>

        <DataTemplate
            x:Key="ChildItemTemplate"
            x:DataType="local:ChildItem">
            <TreeViewItem Content="{x:Bind Text, Mode=OneWay}">
                <TreeViewItem.Resources>
                    <Style TargetType="CheckBox">
                        <Setter Property="IsEnabled" Value="False" />
                        <Setter Property="Opacity" Value="0.0" />
                    </Style>
                </TreeViewItem.Resources>
            </TreeViewItem>
        </DataTemplate>

        <local:TreeViewDataTemplateSelector
            x:Key="TreeViewDataTemplateSelector"
            ChildItemTemplate="{StaticResource ChildItemTemplate}"
            ParentItemTemplate="{StaticResource ParentItemTemplate}" />
    </Page.Resources>

    <TreeView
        ItemTemplateSelector="{StaticResource TreeViewDataTemplateSelector}"
        ItemsSource="{x:Bind Items, Mode=OneWay}"
        SelectionMode="Multiple" />
</Page>

相关问题