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

xaml代码

<UserControl x:Class="SlmenuTest.Tree"
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:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> <UserControl.Resources>
<common:HierarchicalDataTemplate x:Key="Level3Template">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Icon}" Width="16" Height="16"/>
<CheckBox />
<TextBlock Text="{Binding Name}" Foreground="Black" />
</StackPanel>
</common:HierarchicalDataTemplate> <common:HierarchicalDataTemplate x:Key="Level2Template" ItemsSource="{Binding Level3s}" ItemTemplate="{StaticResource Level3Template}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Icon}" Width="16" Height="16"/>
<CheckBox />
<TextBlock Text="{Binding Name}" Foreground="Green"/>
</StackPanel>
</common:HierarchicalDataTemplate> <common:HierarchicalDataTemplate x:Key="Level1Template" ItemsSource="{Binding Level2s}" ItemTemplate="{StaticResource Level2Template}">
<StackPanel Orientation="Horizontal">
<Image Source="{Binding Icon}" />
<CheckBox />
<TextBlock Foreground="Blue" Text="{Binding Name}"/>
</StackPanel>
</common:HierarchicalDataTemplate>
</UserControl.Resources>

后台代码

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<Level1>
{
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<object> e)
{
TreeViewItem item = e.NewValue as TreeViewItem; } } public class Level1
{
public Level1()
{
Level2s = new ObservableCollection<Level2>();
}
public string Name { get; set; }
public string Icon { get; set; }
public ObservableCollection<Level2> Level2s { get; set; }
} public class Level2
{
public Level2()
{
Level3s = new ObservableCollection<Level3>();
} public string Name { get; set; }
public string Icon { get; set; }
public ObservableCollection<Level3> Level3s { get; set; }
} public class Level3
{
public string Name { get; set; }
public string Icon { get; set; }
//public event EventHandler click;
}
}

第二种效果图如下

xaml代码如下

<UserControl x:Class="SLColorPickerDemo.Tree"
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"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="400" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> <UserControl.Resources>
<Style x:Key="RedItemStyle" TargetType="sdk:TreeViewItem">
<Setter Property="HeaderTemplate">
<Setter.Value>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Image Source="images/default.png"/>
<CheckBox />
<TextBlock Text="{Binding}" Foreground="Red" FontStyle="Italic" />
</StackPanel>
</DataTemplate>
</Setter.Value>
</Setter>
<Setter Property="IsExpanded" Value="False" />
</Style>
</UserControl.Resources> <Grid x:Name="LayoutRoot" Background="White">
<sdk:TreeView Margin="5" Grid.Column="0" Grid.Row="1" Name="tvTree" SelectedItemChanged="tvTree_SelectedItemChanged" />
<Border BorderBrush="Gray" BorderThickness="1" Padding="8" Margin="5">
<StackPanel x:Name="DetailsPanel" Margin="4" Width="155">
<StackPanel Orientation="Horizontal">
<TextBlock Text="版块ID: " FontWeight="Bold" />
<TextBlock Text="{Binding ForumID}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="版块名称: " FontWeight="Bold" />
<TextBlock Text="{Binding ForumName}" />
</StackPanel>
<StackPanel Orientation="Horizontal">
<TextBlock Text="版块信息: " FontWeight="Bold" />
<TextBlock x:Name="DetailText" TextWrapping="Wrap" Text="{Binding ForumName}"/>
</StackPanel>
</StackPanel>
</Border>
</Grid>
</UserControl>

后台代码

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
}
}
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<ForumInfo> forumList = new List<ForumInfo>(); public Tree()
{
InitializeComponent();
forumList = GetForumData();
AddTreeNode(, null);
} private void AddTreeNode(int parentID, TreeViewItem treeViewItem)
{
List<ForumInfo> result = (from forumInfo in forumList
where forumInfo.ParendID == parentID
select forumInfo).ToList<ForumInfo>(); if (result.Count > )
{
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<ForumInfo> GetForumData()
{
List<ForumInfo> forumList = new List<ForumInfo>();
forumList.Add(new ForumInfo() { ForumID = , ParendID = , ForumName = "笔记本版块" });
forumList.Add(new ForumInfo() { ForumID = , ParendID = , ForumName = "台式机版块" }); forumList.Add(new ForumInfo() { ForumID = , ParendID = , ForumName = "Dell笔记本" });
forumList.Add(new ForumInfo() { ForumID = , ParendID = , ForumName = "IBM笔记本" });
forumList.Add(new ForumInfo() { ForumID = , ParendID = , ForumName = "IBM-T系列" });
forumList.Add(new ForumInfo() { ForumID = , ParendID = , ForumName = "IBM-R系列" }); forumList.Add(new ForumInfo() { ForumID = , ParendID = , ForumName = "联想台式机" });
forumList.Add(new ForumInfo() { ForumID = , ParendID = , ForumName = "方正台式机" });
forumList.Add(new ForumInfo() { ForumID = , ParendID = , ForumName = "HP台式机" });
forumList.Add(new ForumInfo() { ForumID = , ParendID = , ForumName = "联想家悦H系列" });
forumList.Add(new ForumInfo() { ForumID = , ParendID = , ForumName = "联想IdeaCentre系列" }); return forumList;
} private void tvTree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
{
TreeViewItem item = e.NewValue as TreeViewItem;
ForumInfo fi = item.DataContext as ForumInfo; DetailsPanel.DataContext = fi;
}
}
}

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

Silverlight TreeView 带 checkbox和图片的更多相关文章

  1. WPF 带CheckBox、图标的TreeView

    WPF 带CheckBox.图标的TreeView 在WPF实际项目开发的时候,经常会用到带CheckBox的TreeView,虽然微软在WPF的TreeView中没有提供该功能,但是微软在WPF中提 ...

  2. WPF 带CheckBox、图标的TreeView(转)

    在WPF实际项目开发的时候,经常会用到带CheckBox的TreeView,虽然微软在WPF的TreeView中没有提供该功能,但是微软在WPF中提供强大的ItemTemplate模板功能和自定义样式 ...

  3. 潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据

    原文:潜移默化学会WPF(难点控件treeview)--改造TreeView(CheckBox多选择版本),递归绑定数据 目前自己对treeview的感慨很多 今天先讲 面对这种 表结构的数据 的其中 ...

  4. Android开发学习之路-带文字的图片分享

    有用过微信分享SDK的都应该知道,微信分享到朋友圈的时候是不能同时分享图片和文字的,只要有缩略图,那么文字就不会生效.那么问题就来了,如果我们想把APP内的某些内容连带图片一起分享到微信,是不是没办法 ...

  5. Android实现自定义带文字和图片的Button

    Android实现自定义带文字和图片的Button 在Android开发中经常会需要用到带文字和图片的button,下面来讲解一下常用的实现办法. 一.用系统自带的Button实现 最简单的一种办法就 ...

  6. WPF中Expander控件样式,ListBox的样式(带checkbox)恢复

    Expander控件样式: <ControlTemplate x:Key="ExpanderToggleButton" TargetType="ToggleButt ...

  7. 【转】带checkbox的ListView实现(二)——自定义Checkable控件的实现方法

    原文网址:http://blog.csdn.net/harvic880925/article/details/40475367 前言:前一篇文章给大家展示了传统的Listview的写法,但有的时候我们 ...

  8. 基于jQuery带标题的图片3D切换焦点图

    今天给大家分享一款基于jQuery带标题的图片3D切换焦点图.这款焦点图适用浏览器:IE8.360.FireFox.Chrome.Safari.Opera.傲游.搜狗.世界之窗. 实现的代码. htm ...

  9. [TFRecord格式数据]利用TFRecords存储与读取带标签的图片

    利用TFRecords存储与读取带标签的图片 原创文章,转载请注明出处~ 觉得有用的话,欢迎一起讨论相互学习~Follow Me TFRecords其实是一种二进制文件,虽然它不如其他格式好理解,但是 ...

随机推荐

  1. python开发[第二篇]------str的7个必须掌握的方法以及五个常用方法

    在Python中 基本数据类型有 str int boolean list dict tuple等 其中str的相关方法有30多个 但是常用的就以下7个 join  # split # find # ...

  2. 《Maven实战》笔记-4-生命周期和插件

    除了坐标.依赖以及仓库外,Maven另外两个核心概念是生命周期和插件. 一.生命周期 Maven的生命周期是抽象的,其本身不做任务实际的工作,实际的任务(如编译源代码)都交由插件来完成. 三套生命周期 ...

  3. 利用using和try/finally语句来清理资源

    使用非托管资源的类型必须实现IDisposable接口的Dispose()方法来精确的释放系统资源..Net环境的这一规则使得释放资源代码的职责 是类型的使用者,而不是类型或系统.因此,任何时候你在使 ...

  4. 点石成金:访客至上的网页设计秘笈(原书第2版) 中文PDF版

    可用性设计是Web设计中最重要也是难度最大的一项任务.本书作者根据多年从业的经验,剖析用户的心理,在用户使用的模式.为扫描进行设计.导航设计.主页布局.可用性测试等方面提出了许多独特的观点,并给出了大 ...

  5. VMWare虚拟机无法打开内核设备"\\.\Global\vmx86"的解决方法

    cmd执行: 1.net start vmci 2.net start vmx86 3.net start VMnetuserif

  6. C/C++使用心得:enum与int的相互转换

    如何正确理解enum类型? 例如: enum Color { red, white, blue}; Color x; 我们应说x是Color类型的,而不应将x理解成enumeration类型,更不应将 ...

  7. iOS开发图片与颜色处理工具

    1.根据颜色生成一张图片 /** 根据颜色生成一张图片 @param color 颜色进制 UIColor类型 @return 一张UIImage图片 */ + (UIImage *)createIm ...

  8. shell脚本编程的10个最佳实践

    摘要:每一个在UNIX/Linux上工作的程序员可能都擅长shell脚本编程.对于那些处在shell脚本编程初级阶段的程序员来说,遵循一些恰当的做法可以帮助你更快.更好的学习这些编程技巧. 每一个在U ...

  9. 洛谷P3604 美好的每一天(莫队)

    传送门 由乃的题还是一如既往的可怕…… 先放上原题解 标解: 一个区间可以重排成为回文串,即区间中最多有一个字母出现奇数次,其他的都出现偶数次 发现这个和  类似 这样如果一个区间的  和为  或者  ...

  10. cenos 上的php 支持GD库问题

    ---恢复内容开始--- thinkphp 开发的项目verify类无法引用,原因是没有开启gd库 环境:CentOS 6.4,php-5.3.3需求:php支持GD库解决方案:GD是Linux下的一 ...