博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Silverlight TreeView 带 checkbox和图片
阅读量:6370 次
发布时间:2019-06-23

本文共 9446 字,大约阅读时间需要 31 分钟。

前段时间做Silverlight TreeView 控件,但是要带checkbox和图片,在网上到处找相关的例子,效果图如下

xaml代码

View Code

后台代码

using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Collections.ObjectModel;namespace SlmenuTest{    public partial class Tree : UserControl    {               public Tree()        {            InitializeComponent();            InitTreeData();        }        #region Data        private void InitTreeData()        {            myTree.ItemsSource = new ObservableCollection
{ new Level1 { Name = "test1", Icon="../images/default.png", Level2s = { new Level2 { Name="基础信息", Icon = "../images/search.png", Level3s = { new Level3 { Name = "类别" ,Icon = "../images/computer.png"}, new Level3 { Name = "部门" ,Icon = "../images/computer_on.png"}, new Level3 { Name = "类别2" ,Icon = "../images/exit.png"}, new Level3 { Name = "部门2" ,Icon = "../images/edit.png"} } }, new Level2 { Name="扩展信息", Icon="../images/search.png" } } }, new Level1 { Name="test2", Icon="../images/default.png", Level2s= { new Level2 { Name="报表管理", Icon = "../images/search.png", Level3s = { new Level3 { Name = "报表1" ,Icon = "../images/default.png"}, new Level3 { Name = "报表2" ,Icon = "../images/default.png"}, new Level3 { Name = "报表3" ,Icon = "../images/default.png"}, new Level3 { Name = "报表4",Icon="../images/default.png"} } } } }, new Level1 { Name="test3", Icon="../images/default.png", Level2s= { new Level2 { Name="系统管理", Icon = "../images/search.png", Level3s = { new Level3 { Name = "权限设置" ,Icon = "../images/default.png"}, new Level3 { Name = "用户管理" , Icon = "../images/default.png"} } } } } }; } #endregion private void myTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs
e) { TreeViewItem item = e.NewValue as TreeViewItem; } } public class Level1 { public Level1() { Level2s = new ObservableCollection
(); } public string Name { get; set; } public string Icon { get; set; } public ObservableCollection
Level2s { get; set; } } public class Level2 { public Level2() { Level3s = new ObservableCollection
(); } public string Name { get; set; } public string Icon { get; set; } public ObservableCollection
Level3s { get; set; } } public class Level3 { public string Name { get; set; } public string Icon { get; set; } //public event EventHandler click; }}
View Code

第二种效果图如下

xaml代码如下

View Code

后台代码

using System;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Ink;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.ComponentModel;namespace SLColorPickerDemo{    public class ForumInfo : INotifyPropertyChanged    {        public int ForumID { get; set; }        public int ParendID { get; set; }        public string ForumName { get; set; }        #region INotifyPropertyChanged 成员        public event PropertyChangedEventHandler PropertyChanged;        private void PropertyChaged(string propertyName)        {            PropertyChangedEventHandler handle = PropertyChanged;            if (handle != null)                handle.Invoke(this, new PropertyChangedEventArgs(propertyName));        }        #endregion    }}
View Code
using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Windows;using System.Windows.Controls;using System.Windows.Documents;using System.Windows.Input;using System.Windows.Media;using System.Windows.Media.Animation;using System.Windows.Shapes;using System.Collections.ObjectModel;namespace SLColorPickerDemo{    public partial class Tree : UserControl    {        List
forumList = new List
(); public Tree() { InitializeComponent(); forumList = GetForumData(); AddTreeNode(0, null); } private void AddTreeNode(int parentID, TreeViewItem treeViewItem) { List
result = (from forumInfo in forumList where forumInfo.ParendID == parentID select forumInfo).ToList
(); if (result.Count > 0) { foreach (ForumInfo foruminfo in result) { TreeViewItem objTreeNode = new TreeViewItem(); objTreeNode.Header = foruminfo.ForumName; objTreeNode.DataContext = foruminfo; objTreeNode.ItemContainerStyle = this.Resources["RedItemStyle"] as Style; //添加根节点 if (treeViewItem == null) { tvTree.Items.Add(objTreeNode); //tvTree.ItemContainerStyle = this.Resources["TreeStyle"] as Style; } else { treeViewItem.Items.Add(objTreeNode); } AddTreeNode(foruminfo.ForumID, objTreeNode); } } } public List
GetForumData() { List
forumList = new List
(); forumList.Add(new ForumInfo() { ForumID = 1, ParendID = 0, ForumName = "笔记本版块" }); forumList.Add(new ForumInfo() { ForumID = 2, ParendID = 0, ForumName = "台式机版块" }); forumList.Add(new ForumInfo() { ForumID = 3, ParendID = 1, ForumName = "Dell笔记本" }); forumList.Add(new ForumInfo() { ForumID = 4, ParendID = 1, ForumName = "IBM笔记本" }); forumList.Add(new ForumInfo() { ForumID = 5, ParendID = 4, ForumName = "IBM-T系列" }); forumList.Add(new ForumInfo() { ForumID = 6, ParendID = 4, ForumName = "IBM-R系列" }); forumList.Add(new ForumInfo() { ForumID = 7, ParendID = 2, ForumName = "联想台式机" }); forumList.Add(new ForumInfo() { ForumID = 8, ParendID = 2, ForumName = "方正台式机" }); forumList.Add(new ForumInfo() { ForumID = 9, ParendID = 2, ForumName = "HP台式机" }); forumList.Add(new ForumInfo() { ForumID = 10, ParendID = 7, ForumName = "联想家悦H系列" }); forumList.Add(new ForumInfo() { ForumID = 11, ParendID = 7, ForumName = "联想IdeaCentre系列" }); return forumList; } private void tvTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs
e) { TreeViewItem item = e.NewValue as TreeViewItem; ForumInfo fi = item.DataContext as ForumInfo; DetailsPanel.DataContext = fi; } }}
View Code

 

时间比较长了参考的文章找不到,找到了再把链接贴上。

 

转载于:https://www.cnblogs.com/ZJ199012/p/4024058.html

你可能感兴趣的文章
Jvm(22),回收策略-----标记清除算法
查看>>
MySQL多表关联查询效率高点还是多次单表查询效率高,为什么?
查看>>
UNIX 高手的 10 个习惯
查看>>
传值与传引用
查看>>
HDU 1538 A Puzzle for Pirates(海盗分金问题)
查看>>
C# Web Forms - Using jQuery FullCalendar
查看>>
Sublime-Text-2-pydocstring --- 自动生成python docstring的插件
查看>>
UNIX进程环境
查看>>
学习面试题Day03
查看>>
我最喜欢的jQuery插件模板
查看>>
【云计算】Docker 多进程管理方案
查看>>
[LeetCode] Best Meeting Point 最佳开会地点
查看>>
基于InstallShield2013LimitedEdition的安装包制作
查看>>
【转】从Shell脚本内部将所有标准输出及标准错误显示在屏幕并同时写入文件的方法...
查看>>
iOS开发小技巧--利用MJExtension解决数据结构复杂的模型转换
查看>>
Python中的图形库
查看>>
Linux操作系统分析 ------------------中国科技大学
查看>>
Apache多站点实现原理和配置
查看>>
javascript类型系统——包装对象
查看>>
Android4.4中不能发送SD卡就绪广播
查看>>