翼度科技»论坛 编程开发 .net 查看内容

WPF TreeView 检测SelectedItem变化的简单方案

5

主题

5

帖子

15

积分

新手上路

Rank: 1

积分
15
TreeView无法绑定SelectedItem,而又想知道treeview的selecteditem的变化,当然目前有很多方法,我这里简单的提供一个。
目前主要思路就是通过处理xaml的TreeViewItem的IsSelected属性来进行绑定。
  1.   <TreeView
  2.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />BorderThickness="0"
  3.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />Width="220"
  4.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />ItemsSource="{Binding Items, IsAsync=True}"
  5.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />HorizontalAlignment="Left">
  6.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /><TreeView.ItemContainerStyle>
  7.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
  8.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /></TreeView.ItemContainerStyle>
  9.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /><TreeView.ItemTemplate>
  10.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> <HierarchicalDataTemplate ItemsSource="{Binding Items, IsAsync=True}">
  11.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  <Label Content="{Binding Name}" />
  12.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> </HierarchicalDataTemplate>
  13.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /></TreeView.ItemTemplate>
  14.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  </TreeView>
复制代码
主要部分是
  1.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
复制代码
这里的意思就是绑定到集合泛型的IsSelected属性。
这里呢主要是通过抽象类去实现的也就是:
  1. public abstract class TreeViewItemModel : Notify   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  private bool _IsSelected;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public bool IsSelected   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />get { return _IsSelected; }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />set   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />{   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> _IsSelected = value;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> OnChanged();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> if (value)   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  IChanged changed = ModelChangedManger.Sub.Get();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  if (changed?.Accepted == true)   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />changed.OnModelChanged((T)Convert.ChangeType(this, typeof(T)));   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />}   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> }
复制代码
具体的使用类再去继承即可,也就是
  1. public class GroupModel : TreeViewItemModel   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public GroupModel()   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />Id = Guid.NewGuid();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  private Guid _id;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public Guid Id   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />get { return _id; }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />set   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />{   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> _id = value;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> OnChanged();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />}   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  private string _name;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public string Name   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />get { return _name; }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />set   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />{   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> _name = value;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> OnChanged();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />}   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  private ObservableCollection _Item = new ObservableCollection();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public ObservableCollection Items   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />get { return _Item; }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />set   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />{   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> _Item = value;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> OnChanged();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />}   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> }
复制代码
处理完这样,我们只是完成了一部分。这里这来说下
  1.          if (value)   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  IChanged changed = ModelChangedManger.Sub.Get();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  if (changed?.Accepted == true)   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />changed.OnModelChanged((T)Convert.ChangeType(this, typeof(T)));   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> }
复制代码
这个是干什么的。
很明显,Isslected属性触发,我们想通知viewmodel,treeview的selecteditem属性变了,或者叫做selectindex也变了的意思。所以简单的实现了一个事件聚合器,这个部分代码比较简单,整体如下
  1.    <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> public interface IChanged   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  void OnModelChanged(T Model);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  bool Accepted { get; set; }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> public sealed class ModelChangedManger   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  private ModelChangedManger()   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  static ModelChangedManger() { }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  private class Inner   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />private Inner() { }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />internal readonly static ModelChangedManger manger = new ModelChangedManger();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  private ConcurrentDictionary Keys { get; set; } = new ConcurrentDictionary();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public IChanged Get()   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />if (Keys.TryGetValue(typeof(T), out object val))   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />{   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> return val as IChanged;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />}   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />return null;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public void Set(IChanged model)   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />if (!Keys.ContainsKey(typeof(T)))   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />{   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> Keys.TryAdd(typeof(T), model);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />}   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public void Remove()   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />Keys.TryRemove(typeof(T), out _);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public static ModelChangedManger Sub   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />get => Inner.manger;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> }
复制代码
主要是Ichanged接口和manger的一起搭配使用。使用字典将要引发和被引发的类型,类存储起来,这样就可以全局或者大范围的使用了。
同理 我们需要viewmodel上继承一个引发事件的ichanged类
  1.   public abstract class TreeViewModel : Notify, IChanged   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public virtual void OnModelChanged(T Model)   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />SelectedItem = Model;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  private bool _Accepted;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public bool Accepted   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />get { return _Accepted; }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />set   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />{   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> _Accepted = value;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> OnChanged();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />}   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  private T _SelectedItem;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public T SelectedItem   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />get { return _SelectedItem; }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />set   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />{   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> _SelectedItem = value;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> OnChanged();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />}   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> public class ViewModel : TreeViewModel   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  private System.Collections.ObjectModel.ObservableCollection _list = new System.Collections.ObjectModel.ObservableCollection();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public System.Collections.ObjectModel.ObservableCollection Items   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />get { return _list; }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />set   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />{   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> _list = value;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> OnChanged();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />}   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  public ViewModel()   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  {   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />GroupModel groupModel1 = new GroupModel();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel1.Name = "Ken";   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />GroupModel groupModel_1 = new GroupModel();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel_1.Name = "A";   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />GroupModel groupModel_2 = new GroupModel();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel_2.Name = "B";   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />GroupModel groupModel_3 = new GroupModel();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel_3.Name = "C";   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />GroupModel groupModel_4 = new GroupModel();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel_4.Name = "D";   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel1.Items.Add(groupModel_1);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel1.Items.Add(groupModel_2);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel1.Items.Add(groupModel_3);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel1.Items.Add(groupModel_4);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />GroupModel groupModel2 = new GroupModel();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel2.Name = "TOM";   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />GroupModel groupModel_5 = new GroupModel();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel_5.Name = "a";   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />GroupModel groupModel_6 = new GroupModel();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel_6.Name = "b";   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />GroupModel groupModel_7 = new GroupModel();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel_7.Name = "c";   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />GroupModel groupModel_8 = new GroupModel();   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel_8.Name = "d";   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel2.Items.Add(groupModel_5);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel2.Items.Add(groupModel_6);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel2.Items.Add(groupModel_7);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />groupModel2.Items.Add(groupModel_8);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />Items.Add(groupModel1);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />Items.Add(groupModel2);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />ModelChangedManger.Sub.Set(this);   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />this.Accepted = true;   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />  }   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /> }
复制代码
这样,就可以简单的实现了对selectedItem的变化的监控了
源代码连接

来源:https://www.cnblogs.com/T-ARF/archive/2023/07/14/17553107.html
免责声明:由于采集信息均来自互联网,如果侵犯了您的权益,请联系我们【E-Mail:cb@itdo.tech】 我们会及时删除侵权内容,谢谢合作!

举报 回复 使用道具