list作为数据源网上实例比较多,但是Dictionary作为数据源,网上很少。其实两者使用基本上一样,主要差异在于数据绑定的时候,使用Value.属性
直接上xaml
<Window x:Class="MvvmTree.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:MvvmTree"
mc:Ignorable="d"
Title="MainWindow" Height="450" Width="800" MouseMove="Window_MouseMove">
<Grid Height="400" Width="400">
<Popup x:Name="my_popup" StaysOpen="False" Placement="Mouse" AllowsTransparency="True">
<TextBlock Text="1111111" Background="Transparent"></TextBlock>
</Popup>
<Image Source="ball_normal.png" x:Name="BallImage" Visibility="Collapsed"></Image>
<Image Source="camera_normal.png" x:Name="CameraImage" Visibility="Collapsed"></Image>
<Image Source="direct.png" x:Name="DirectImage" Visibility="Collapsed"></Image>
<TreeView ItemsSource="{Binding MyTrees}" Width="200" Height="300" Name="tree" VirtualizingStackPanel.IsVirtualizing="True"
VirtualizingStackPanel.VirtualizationMode="Recycling" SelectedItemChanged="Tree_SelectedItemChanged" MouseDoubleClick="Tree_MouseDoubleClick" AllowDrop="True" MouseDown="Tree_MouseDown" MouseMove="Tree_MouseMove"
>
<TreeView.ItemTemplate>
<HierarchicalDataTemplate ItemsSource="{Binding Value.Children}">
<StackPanel Orientation="Horizontal" Background="{Binding bk}">
<Image VerticalAlignment="Center" Stretch="Uniform" Source="{Binding Value.ImageSource}">
</Image>
<TextBlock Text="{Binding Value.ItemName}" FontSize="20"></TextBlock>
</StackPanel>
</HierarchicalDataTemplate>
</TreeView.ItemTemplate>
<TreeView.ItemContainerStyle>
<Style TargetType="{x:Type TreeViewItem}">
<Setter Property="IsExpanded" Value="True"></Setter>
</Style>
</TreeView.ItemContainerStyle>
</TreeView>
<Button Content="Button" HorizontalAlignment="Left" Margin="521,205,-196,0" VerticalAlignment="Top" Width="75" Click="Button_Click"/>
<Label Content="Label" HorizontalAlignment="Left" Margin="-152,156,0,0" VerticalAlignment="Top" Height="104" Width="98" MouseUp="Label_MouseUp"/>
</Grid>
</Window>
类的定义如下:
using Common;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media;
namespace MvvmTree
{
class MyTree_Vm: ObservableObject
{
Dictionary<string, MyTree> _MyTrees;
public Dictionary<string, MyTree> MyTrees
{
get
{
return _MyTrees;
}
set
{
UpdateProperty(ref _MyTrees, value);
}
}
}
class MyTree : ObservableObject
{
Dictionary<string, MyTree> _Children = new Dictionary<string, MyTree>();
public Dictionary<string, MyTree> Children
{
get
{
return _Children;
}
set
{
UpdateProperty(ref _Children, value);
}
}
public string ItemName
{
get;
set;
}
public string ResId
{
get;
set;
}
public ImageSource ImageSource
{
get;
set;
}
SolidColorBrush _bk = null;
public SolidColorBrush bk
{
get
{
return _bk;
}
set
{
UpdateProperty(ref _bk, value);
}
}
}
}
效果如下: