日涨百分一预测验证 发表于 2023-7-14 14:43:15

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

TreeView无法绑定SelectedItem,而又想知道treeview的selecteditem的变化,当然目前有很多方法,我这里简单的提供一个。
目前主要思路就是通过处理xaml的TreeViewItem的IsSelected属性来进行绑定。
<TreeView
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <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"
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <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"
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <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}"
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <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">
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <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>
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <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>
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <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>
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <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}">
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <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}" />
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <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>
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <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>
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" /></TreeView>主要部分是
   <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />这里的意思就是绑定到集合泛型的IsSelected属性。
这里呢主要是通过抽象类去实现的也就是:
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}" /> }具体的使用类再去继承即可,也就是
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}" /> }处理完这样,我们只是完成了一部分。这里这来说下
         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也变了的意思。所以简单的实现了一个事件聚合器,这个部分代码比较简单,整体如下
   <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类
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】 我们会及时删除侵权内容,谢谢合作!
页: [1]
查看完整版本: WPF TreeView 检测SelectedItem变化的简单方案