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);}}}
}
效果如下:
Wpf之Tree使用Dictionary作为数据源相关推荐
- Dictionary作为数据源绑定,调用c++库中返回为BYTE*的函数,listView项排序
最近在做一个电子档案管理的项目.现在还处于初期,只是做一个简单demo拿去跟客户演示.至于最后谈不谈得下来,到底做不做,反正我是不看好,但没因为这样就马马虎虎.草草了事.这个项目算是b/s加c/s混合 ...
- WPF中binding一个dictionary
参考Stack Overflow 界面上的绑定必须加上Path=,不加绑定就失败了.其次,我这里是一个key为枚举的字典,因此必须给类型名,且必须写上类的路径.xmlns:MyNameSpace=&q ...
- WPF Logical Tree vs. Visual Tree
Moved to http://blog.tangcs.com/2008/07/19/wpf-logical-tree-vs-visual-tree/ 转载于:https://www.cnblogs. ...
- COMBOBOX绑定DICTIONARY做为数据源
ComboBox的DataSource的值可设置为 :实现 IList 接口的对象,如 DataSet 或 Array.默认为 空引用(在 Visual Basic 中为 Nothing). 但是在实 ...
- 基于WPF的开发的知识点
基于WPF的开发 一.XAML 语言 XAML被编译为BAML(Binary Application Markup Language)文件.通常,BAML文件比XAML更小,编译后的BAML都是Pre ...
- springboot多数据源动态数据源(主从)
多数据源 使用Spring Boot时,默认情况下,配置DataSource非常容易.Spring Boot会自动为我们配置好一个DataSource. 如果在application.yml中指定了s ...
- linux之一些比较新但是常用的命令(expr ag tree cloc stat tmux axel)
1 expr命令 介绍:这个命令用来匹配正则表达式,这个命令linux系统自带,不信你自己试下 使用:expr 正则表达式 输出结果 expr http:\/\/www\.baidu\.com h ...
- 报表开发工具!DevExpress Reporting v19.1:WPF/Web平台报表
行业领先的.NET界面控件DevExpress Reporting全新发布了v19.1版本,本文主要为大家介绍WPF.Web平台中DevExpress Reporting发布的一些新功能及增强部分功能 ...
- 分享Silverlight 3D开源项目和Silverlight/WPF/Windows Phone一周学习导读(4月25日-4月29日)...
Silverlight 5 Beta版本中最引人注目的特性是3D功能,Silverlight 5与XNA Framework的结合,使Silverlight完美支持3D效果. 在微软MIX11大会后, ...
最新文章
- 对第三方 SDK 依赖冲突,重新打个包试试
- 标 题:[转帖]SQL Server日期计算(收藏)
- @RequestMapping中method的默认值是什么?
- Anaconda中快速安装Tensorflow与Keras并在pycharm中完成相应配置(win10cpu版)
- Java线程详解(7)-线程的调度
- 综合应用题:多线程复制文件(知识点:多线程、随机读写流)
- AndroidStudio创建第一个 Flutter 应用程序
- 输出 100 万字、维护 3 个GitHub 项目,技术大佬的 2019 太燃了……
- [SPOJ - FTOUR2] Free tour II(点分治 + 背包dp + 启发式合并)
- Google 搜索老矣,尚能饭否?
- 链上链下协同的数据归档
- win10 操作无法完成,因为文件已在windows资源管理器打开
- MySQL从删库到跑路(3):神奇的select
- springboot基于微信小程序“智慧校园” 一体式的设计与实现毕业设计源码091634
- 蓝牙初学者要知道的几件事
- 2023年徐汇区文化发展专项资金扶持项目申报指南
- iOS安全攻与防(总篇)
- 解决oracle异常ORA-01012: not logged on
- 人工智能(AI)背后人工的力量——数据标注
- 实现74HC85的功能以及16位数值比较器