0

我正在尝试在 WinUI3 桌面应用程序中动态生成树视图。它可以生成树,但默认情况下所有节点都未选中。我的应用程序需要记住选择并复制以前的状态。

从技术上讲,我可以从中读取选择状态TreeView.SelectedNodes。但是我无法找到从代码中选择节点的方法。

我在网上找到了几篇关于 WPF 或 UWP 的相关文章,但没有针对 WinUI3。

环境

  • WinUI3桌面
  • Windows 应用 SDK 1.0.0
  • MvvmGen 1.1.2

目标

  • 在启动时选择树中的一些项目。
  • 从 ViewModel 中读取选择的状态。

问题

  • 无法将视图中的 IsSelected 属性绑定到ListViewItem.IsSelected

代码

MainWindow.xaml

<Window
    x:Class="WinUITreeViewTest.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:WinUITreeViewTest"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d">

    <StackPanel DataContext="MainWindowViewModel">
        <TreeView Name="MyItemView" SelectionMode="Multiple" ItemsSource="{Binding MyItems}">
            <TreeView.ItemTemplate>
                <DataTemplate x:DataType="local:MyItem">
                    <TreeViewItem ItemsSource="{Binding Children}" Content="{Binding Name}" IsExpanded="{Binding IsExpanded}" IsSelected="{Binding IsSelected}"/>
                </DataTemplate>
            </TreeView.ItemTemplate>
        </TreeView>
    </StackPanel>
</Window>

TreeViewItem节点中,它旨在绑定MyItem.IsSelectedTreeViewItem.IsSelcted. 是行不通的。

MainWindow.xaml.cs

public sealed partial class MainWindow : Window
{
    public MainWindow()
    {
        this.InitializeComponent();

        ViewModel = new MainWindowViewModel();
        MyItemView.DataContext = ViewModel;

        ObservableCollection<MyItem> root = new ObservableCollection<MyItem>();

        MyItem item1 = new MyItem() { Name = "Item1", IsExpanded=true };
        root.Add(item1);
        item1.Children.Add(new MyItem() { Name = "Item1.1" });
        item1.Children.Add(new MyItem() { Name = "Item1.2" });
        item1.Children.Add(new MyItem() { Name = "Item1.3", IsSelected=true });

        MyItem item2 = new MyItem() { Name = "Item2", IsExpanded = true };
        root.Add(item2);
        item2.Children.Add(new MyItem() { Name = "Item2.1" });
        item2.Children.Add(new MyItem() { Name = "Item2.2" });
        item2.Children.Add(new MyItem() { Name = "Item2.3" });

        MyItem item3 = new MyItem() { Name = "Item3", IsExpanded = true };
        root.Add(item3);
        item3.Children.Add(new MyItem() { Name = "Item3.1" });
        item3.Children.Add(new MyItem() { Name = "Item3.2" });
        item3.Children.Add(new MyItem() { Name = "Item3.3" });

        ViewModel.MyItems = root;
    }

    MainWindowViewModel ViewModel;
}
MainWindowViewModel.cs

[ViewModel]
public partial class MainWindowViewModel
{
    [Property]
    ObservableCollection<MyItem> myItems;
}

[ViewModel]
public partial class MyItem
{
    [Property]
    private string name;

    public override string ToString() => Name;

    [Property]
    private bool? isSelected;

    [Property]
    private bool isExpanded;

    [Property]
    private ObservableCollection<MyItem> children = new ObservableCollection<MyItem>();
}

MvvmGen 像这样自动为 MVVM 生成相应的属性。

public bool? IsSelected
{
    get => isSelected;
    set
    {
        if (isSelected != value)
        {
            isSelected = value;
            OnPropertyChanged("IsSelected");
        }
    }
}

参考:https ://github.com/hayashida-katsutoshi/WinUITreeViewTest

4

1 回答 1

0

这是一个已知问题。我在 GitHub 上找到了一张票。

https://github.com/microsoft/microsoft-ui-xaml/issues/125

于 2021-11-18T22:23:03.517 回答