Wpf之Tree使用Dictionary作为数据源

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);
            }
        }


    }
}

效果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值