[英]How to bind a property to TreeViewItem.IsSelected in WinUI3?
I am trying to generate a tree view dynamically in WinUI3 desktop application.我正在尝试在 WinUI3 桌面应用程序中动态生成树视图。 It can generate the tree but all nodes are unselected by default.
它可以生成树,但默认情况下所有节点都未选中。 My application need to remember selection and replicate previous status.
我的应用程序需要记住选择并复制以前的状态。
Technically I am able to read selection status from TreeView.SelectedNodes
.从技术上讲,我能够从
TreeView.SelectedNodes
读取选择状态。 However I am not able to find the way to select an node from the code.但是我无法从代码中找到通往 select 节点的方法。
I found couple of related articles for WPF or UWP on the net, but not for WinUI3.我在网上找到了几篇关于 WPF 或 UWP 的相关文章,但不是针对 WinUI3。
ListViewItem.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>
In the TreeViewItem
node, it is intended to bind MyItem.IsSelected
to TreeViewItem.IsSelcted
.在
TreeViewItem
节点中,它旨在将MyItem.IsSelected
绑定到TreeViewItem.IsSelcted
。 Is does not work.是行不通的。
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 generates corresponding property for MVVM automatically like this. MvvmGen 像这样自动为 MVVM 生成相应的属性。
public bool? IsSelected
{
get => isSelected;
set
{
if (isSelected != value)
{
isSelected = value;
OnPropertyChanged("IsSelected");
}
}
}
Reference: https://github.com/hayashida-katsutoshi/WinUITreeViewTest参考: https://github.com/hayashida-katsutoshi/WinUITreeViewTest
This was a known issue.这是一个已知问题。 I found a ticket in GitHub.
我在 GitHub 找到了一张票。
https://github.com/microsoft/microsoft-ui-xaml/issues/125 https://github.com/microsoft/microsoft-ui-xaml/issues/125
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.