最近项目上需要通过MVVM来控制TreeView,其中需要需要控制通过搜索来定位某个节点,正常逻辑下,首先通过需要在树上面找到该节点,然后选中该节点,并将该节点的父节点展开,这个时候需要通过MVVM来控制,需要绑定起来,只是一直没有binding上,代码如下:
MVVM示例代码:
1 using System; 2 using System.Collections.Generic; 3 using System.Collections.ObjectModel; 4 using System.Linq; 5 using System.Text; 6 using System.Threading.Tasks; 7 8 namespace DragDrop 9 { 10 class DataItem : NotifyPropertyBase, ICloneable 11 { 12 public DataItem(string header, int deepth = 1) 13 { 14 Header = header; 15 Deepth = deepth; 16 } 17 18 public object Clone() 19 { 20 DataItem dataItem = new DataItem(Header, Deepth); 21 dataItem.IsExpanded = IsExpanded; 22 dataItem.IsSelected = IsSelected; 23 dataItem.Deepth = Deepth; 24 foreach (DataItem item in Items) 25 dataItem.Items.Add((DataItem)item.Clone()); 26 return dataItem; 27 } 28 29 private string header; 30 public string Header 31 { 32 get { return header; } 33 set 34 { 35 SetProperty(ref header, value); 36 } 37 } 38 39 private bool isSelected; 40 public bool IsSelected 41 { 42 get { return isSelected; } 43 set { SetProperty(ref isSelected, value); } 44 } 45 46 private bool isExpanded; 47 public bool IsExpanded 48 { 49 get { return isExpanded; } 50 set 51 { 52 SetProperty(ref isExpanded, value); 53 Console.WriteLine("{0}--{1}", Header, IsExpanded); 54 } 55 } 56 57 private int deepth; 58 public int Deepth 59 { 60 get { return deepth; } 61 set 62 { 63 if (deepth != value) 64 { 65 deepth = value; 66 SetProperty(ref deepth, value); 67 } 68 } 69 } 70 71 private ObservableCollection<DataItem> mItems = null; 72 public ObservableCollection<DataItem> Items 73 { 74 get 75 { 76 if (mItems == null) 77 mItems = new ObservableCollection<DataItem>(); 78 return mItems; 79 } 80 } 81 82 } 83 84 class Data 85 { 86 private static Data mInstance = new Data(); 87 88 public static Data Instance 89 { 90 get { return mInstance; } 91 } 92 93 private ObservableCollection<DataItem> GenerateTreeViewItems() 94 { 95 ObservableCollection<DataItem> items = new ObservableCollection<DataItem>(); 96 97 DataItem item1 = new DataItem("TreeViewItem1"); 98 item1.Items.Add(new DataItem("SubItem1", item1.Deepth)); 99 item1.Items.Add(new DataItem("SubItem2", item1.Deepth)); 100 item1.Items.Add(new DataItem("SubItem3", item1.Deepth)); 101 item1.Items.Add(new DataItem("SubItem4", item1.Deepth)); 102 items.Add(item1); 103 104 DataItem item2 = new DataItem("TreeViewItem2"); 105 item2.Items.Add(new DataItem("SubItem1", item2.Deepth)); 106 item2.Items.Add(new DataItem("SubItem2", item2.Deepth)); 107 items.Add(item2); 108 109 DataItem item3 = new DataItem("TreeViewItem3"); 110 item3.Items.Add(new DataItem("SubItem1", item3.Deepth)); 111 item3.Items.Add(new DataItem("SubItem2", item3.Deepth)); 112 item3.Items.Add(new DataItem("SubItem3", item3.Deepth)); 113 item3.Items.Add(new DataItem("SubItem4", item3.Deepth)); 114 item3.Items.Add(new DataItem("SubItem5", item3.Deepth)); 115 item3.Items.Add(new DataItem("SubItem6", item3.Deepth)); 116 item3.Items.Add(new DataItem("SubItem7", item3.Deepth)); 117 item3.Items.Add(new DataItem("SubItem8", item3.Deepth)); 118 items.Add(item3); 119 120 return items; 121 } 122 123 private ObservableCollection<DataItem> GenerateListItems() 124 { 125 ObservableCollection<DataItem> items = new ObservableCollection<DataItem>(); 126 items.Add(new DataItem("ListBoxItem1")); 127 items.Add(new DataItem("ListBoxItem2")); 128 items.Add(new DataItem("ListBoxItem3")); 129 items.Add(new DataItem("ListBoxItem4")); 130 items.Add(new DataItem("ListBoxItem5")); 131 return items; 132 } 133 134 public ObservableCollection<DataItem> TreeViewItems 135 { 136 get 137 { 138 if (mTreeViewItems == null) 139 mTreeViewItems = GenerateTreeViewItems(); 140 return mTreeViewItems; 141 } 142 } 143 144 public ObservableCollection<DataItem> ListBoxItems 145 { 146 get 147 { 148 if (mListBoxItems == null) 149 mListBoxItems = GenerateListItems(); 150 return mListBoxItems; 151 } 152 } 153 154 private ObservableCollection<DataItem> mTreeViewItems = null; 155 private ObservableCollection<DataItem> mListBoxItems = null; 156 } 157 }
界面代码:
1 <Window x:Class="DragDrop.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 5 xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 6 xmlns:local="clr-namespace:DragDrop" 7 mc:Ignorable="d" 8 Title="MainWindow" Height="350" Width="525"> 9 <Window.Resources> 10 <HierarchicalDataTemplate x:Key="treeViewTemplate" DataType="{x:Type local:DataItem}" ItemsSource="{Binding Items}"> 11 <TextBlock Text="{Binding Header}"/> 12 </HierarchicalDataTemplate> 13 14 <Style x:Key="TreeViewStyle" TargetType="{x:Type TreeViewItem}"> 15 <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/> 16 <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/> 17 <Style.Triggers> 18 <DataTrigger Binding="{Binding Deepth}" Value="1"> 19 <Setter Property="IsExpanded" Value="True"/> 20 </DataTrigger> 21 </Style.Triggers> 22 </Style> 23 </Window.Resources> 24 <Grid x:Name="mTopLevelGrid"> 25 <TreeView x:Name="mTreeView" Grid.Column="0" 26 ItemsSource="{Binding Source={x:Static local:Data.Instance}, Path=TreeViewItems}" 27 ItemTemplate="{StaticResource treeViewTemplate}" 28 ItemContainerStyle="{StaticResource TreeViewStyle}"/> 29 </Grid> 30 </Window>
数据的绑定没有问题,界面没有问题,奇怪的是IsSelected可以正常绑定,但是IsExpanded就是不行,后来发现这两个属性唯一的区别就是在TreeView的式样中
1 <Style.Triggers> 2 <DataTrigger Binding="{Binding Deepth}" Value="1"> 3 <Setter Property="IsExpanded" Value="True"/> 4 </DataTrigger> 5 </Style.Triggers>
后面把这个DataTrigger这段代码注释掉,编译一下,重新运行,IsExpanded就可以正常绑定。后面自己思考了一下,MVVM模式实现将ViewModel与界面上面的显示绑定起来,实际上也就是使用了观察者模式,而Trigger的原理应该和数据binding是一样的,而且这个Trigger写在后面,可能直接把前面的绑定取代了,但是试了一下,将DataTrigger放到前面,也还是绑定不上,这种可能性排除;个人猜测可能是Trigger的优先级比较高,从而造成IsExpanded一直Binding不上去。
原文地址:https://www.cnblogs.com/Johar/p/9886646.html
时间: 2024-10-14 00:33:08