最近项目上需要通过MVVM来控制TreeView,其中需要需要控制通过搜索来定位某个节点,正常逻辑下,首先通过需要在树上面找到该节点,然后选中该节点,并将该节点的父节点展开,这个时候需要通过MVVM来控制,需要绑定起来,只是一直没有binding上,代码如下:
MVVM示例代码:
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks; namespace DragDrop
{
class DataItem : NotifyPropertyBase, ICloneable
{
public DataItem(string header, int deepth = )
{
Header = header;
Deepth = deepth;
} public object Clone()
{
DataItem dataItem = new DataItem(Header, Deepth);
dataItem.IsExpanded = IsExpanded;
dataItem.IsSelected = IsSelected;
dataItem.Deepth = Deepth;
foreach (DataItem item in Items)
dataItem.Items.Add((DataItem)item.Clone());
return dataItem;
} private string header;
public string Header
{
get { return header; }
set
{
SetProperty(ref header, value);
}
} private bool isSelected;
public bool IsSelected
{
get { return isSelected; }
set { SetProperty(ref isSelected, value); }
} private bool isExpanded;
public bool IsExpanded
{
get { return isExpanded; }
set
{
SetProperty(ref isExpanded, value);
Console.WriteLine("{0}--{1}", Header, IsExpanded);
}
} private int deepth;
public int Deepth
{
get { return deepth; }
set
{
if (deepth != value)
{
deepth = value;
SetProperty(ref deepth, value);
}
}
} private ObservableCollection<DataItem> mItems = null;
public ObservableCollection<DataItem> Items
{
get
{
if (mItems == null)
mItems = new ObservableCollection<DataItem>();
return mItems;
}
} } class Data
{
private static Data mInstance = new Data(); public static Data Instance
{
get { return mInstance; }
} private ObservableCollection<DataItem> GenerateTreeViewItems()
{
ObservableCollection<DataItem> items = new ObservableCollection<DataItem>(); DataItem item1 = new DataItem("TreeViewItem1");
item1.Items.Add(new DataItem("SubItem1", item1.Deepth));
item1.Items.Add(new DataItem("SubItem2", item1.Deepth));
item1.Items.Add(new DataItem("SubItem3", item1.Deepth));
item1.Items.Add(new DataItem("SubItem4", item1.Deepth));
items.Add(item1); DataItem item2 = new DataItem("TreeViewItem2");
item2.Items.Add(new DataItem("SubItem1", item2.Deepth));
item2.Items.Add(new DataItem("SubItem2", item2.Deepth));
items.Add(item2); DataItem item3 = new DataItem("TreeViewItem3");
item3.Items.Add(new DataItem("SubItem1", item3.Deepth));
item3.Items.Add(new DataItem("SubItem2", item3.Deepth));
item3.Items.Add(new DataItem("SubItem3", item3.Deepth));
item3.Items.Add(new DataItem("SubItem4", item3.Deepth));
item3.Items.Add(new DataItem("SubItem5", item3.Deepth));
item3.Items.Add(new DataItem("SubItem6", item3.Deepth));
item3.Items.Add(new DataItem("SubItem7", item3.Deepth));
item3.Items.Add(new DataItem("SubItem8", item3.Deepth));
items.Add(item3); return items;
} private ObservableCollection<DataItem> GenerateListItems()
{
ObservableCollection<DataItem> items = new ObservableCollection<DataItem>();
items.Add(new DataItem("ListBoxItem1"));
items.Add(new DataItem("ListBoxItem2"));
items.Add(new DataItem("ListBoxItem3"));
items.Add(new DataItem("ListBoxItem4"));
items.Add(new DataItem("ListBoxItem5"));
return items;
} public ObservableCollection<DataItem> TreeViewItems
{
get
{
if (mTreeViewItems == null)
mTreeViewItems = GenerateTreeViewItems();
return mTreeViewItems;
}
} public ObservableCollection<DataItem> ListBoxItems
{
get
{
if (mListBoxItems == null)
mListBoxItems = GenerateListItems();
return mListBoxItems;
}
} private ObservableCollection<DataItem> mTreeViewItems = null;
private ObservableCollection<DataItem> mListBoxItems = null;
}
}
界面代码:
<Window x:Class="DragDrop.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:DragDrop"
mc:Ignorable="d"
Title="MainWindow" Height="" Width="">
<Window.Resources>
<HierarchicalDataTemplate x:Key="treeViewTemplate" DataType="{x:Type local:DataItem}" ItemsSource="{Binding Items}">
<TextBlock Text="{Binding Header}"/>
</HierarchicalDataTemplate> <Style x:Key="TreeViewStyle" TargetType="{x:Type TreeViewItem}">
<Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"/>
<Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"/>
<Style.Triggers>
<DataTrigger Binding="{Binding Deepth}" Value="">
<Setter Property="IsExpanded" Value="True"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Window.Resources>
<Grid x:Name="mTopLevelGrid">
<TreeView x:Name="mTreeView" Grid.Column=""
ItemsSource="{Binding Source={x:Static local:Data.Instance}, Path=TreeViewItems}"
ItemTemplate="{StaticResource treeViewTemplate}"
ItemContainerStyle="{StaticResource TreeViewStyle}"/>
</Grid>
</Window>
数据的绑定没有问题,界面没有问题,奇怪的是IsSelected可以正常绑定,但是IsExpanded就是不行,后来发现这两个属性唯一的区别就是在TreeView的式样中
<Style.Triggers>
<DataTrigger Binding="{Binding Deepth}" Value="">
<Setter Property="IsExpanded" Value="True"/>
</DataTrigger>
</Style.Triggers>
后面把这个DataTrigger这段代码注释掉,编译一下,重新运行,IsExpanded就可以正常绑定。后面自己思考了一下,MVVM模式实现将ViewModel与界面上面的显示绑定起来,实际上也就是使用了观察者模式,而Trigger的原理应该和数据binding是一样的,而且这个Trigger写在后面,可能直接把前面的绑定取代了,但是试了一下,将DataTrigger放到前面,也还是绑定不上,这种可能性排除;个人猜测可能是Trigger的优先级比较高,从而造成IsExpanded一直Binding不上去。