在web开发中,带checkbox的tree是一个很有用的东东,比如权限选择、分类管理,如果不用sl,单纯用js+css实现是很复杂的,有了SL之后,就变得很轻松了

解决方案一:
利用Silvelright ToolKit(微软的开源项目),项目地址http://silverlight.codeplex.com/
在线演示地址:http://silverlight.net/content/samples/sl4/toolkitcontrolsamples/run/default.html

解决方案二:
telerik公司的Rad for Silverlight商业控件(收费控件)
在线演示地址 http://demos.telerik.com/silverlight/

不管用哪一种方案,代码都是差不多的,为了实现数据绑定,先创建一个silverlight类库项目BusinessObject,定义数据项实体:
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Markup;namespace BusinessObject
{/// <summary>/// 地区数据项/// </summary>[ContentProperty("Children")]//指示Children属性是 XAML 的Content内容属性public class PlaceItem : INotifyPropertyChanged{/// <summary>/// 构造函数/// </summary>public PlaceItem(){Children = new Collection<PlaceItem>();IsSelected = true;}/// <summary>/// 地区名称/// </summary>public string Name { get; set; }/// <summary>/// 地区描述/// </summary>public string Description { get; set; }/// <summary>/// 得到下级元素容器/// </summary>public Collection<PlaceItem> Children { get; private set; }/// <summary>/// 是否有子项/// </summary>public bool HasChild{get{return Children.Count > 0;}}/// <summary>/// 是否选中/// </summary>private bool? _isSelected;/// <summary>/// 该特性是否想要被安装/// </summary>public bool? IsSelected{get{return _isSelected;}set{if (value != _isSelected){_isSelected = value;OnPropertyChanged("IsSelected");}}}public event PropertyChangedEventHandler PropertyChanged;/// <summary>/// 属性改变时触发事件/// </summary>/// <param name="propertyName">Property that changed.</param>private void OnPropertyChanged(string propertyName){PropertyChangedEventHandler handler = PropertyChanged;if (null != handler){handler.Invoke(this, new PropertyChangedEventArgs(propertyName));}}}
}

然后再定义一个 演示数据集合类:
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Markup;namespace BusinessObject
{/// <summary>/// 地区数据项/// </summary>[ContentProperty("Children")]//指示Children属性是 XAML 的Content内容属性public class PlaceItem : INotifyPropertyChanged{/// <summary>/// 构造函数/// </summary>public PlaceItem(){Children = new Collection<PlaceItem>();IsSelected = true;}/// <summary>/// 地区名称/// </summary>public string Name { get; set; }/// <summary>/// 地区描述/// </summary>public string Description { get; set; }/// <summary>/// 得到下级元素容器/// </summary>public Collection<PlaceItem> Children { get; private set; }/// <summary>/// 是否有子项/// </summary>public bool HasChild{get{return Children.Count > 0;}}/// <summary>/// 是否选中/// </summary>private bool? _isSelected;/// <summary>/// 该特性是否想要被安装/// </summary>public bool? IsSelected{get{return _isSelected;}set{if (value != _isSelected){_isSelected = value;OnPropertyChanged("IsSelected");}}}public event PropertyChangedEventHandler PropertyChanged;/// <summary>/// 属性改变时触发事件/// </summary>/// <param name="propertyName">Property that changed.</param>private void OnPropertyChanged(string propertyName){PropertyChangedEventHandler handler = PropertyChanged;if (null != handler){handler.Invoke(this, new PropertyChangedEventArgs(propertyName));}}}
}

好了,开始干正事儿了:
toolkit中的treeview用法
xaml部分
<UserControl x:Class="ToolKit.MainPage"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"xmlns:toolkit="clr-namespace:System.Windows.Controls;assembly=System.Windows.Controls.Toolkit" xmlns:local="clr-namespace:BusinessObject;assembly=BusinessObject"xmlns:common="clr-namespace:System.Windows;assembly=System.Windows.Controls"><Grid x:Name="LayoutRoot" Background="White"><Grid.Resources><!--定义数据源--><local:SampleData x:Key="SampleDataSource"></local:SampleData><!--定义模板--><common:HierarchicalDataTemplate x:Key="NodeTemplate"  ItemsSource="{Binding Children}"><StackPanel Orientation="Horizontal" ToolTipService.ToolTip="{Binding Description}"><CheckBoxIsTabStop="False"                        IsThreeState="{Binding HasChild}"IsChecked="{Binding IsSelected, Mode=TwoWay}"Click="ItemCheckbox_Click"/><ContentPresenter Content="{Binding Name}" /></StackPanel></common:HierarchicalDataTemplate></Grid.Resources><sdk:TreeView  Name="treeView1" ItemTemplate="{StaticResource NodeTemplate}"ItemsSource="{Binding Source={StaticResource SampleDataSource}, Path=SamplePlaceItemCollection}" Margin="10" BorderThickness="0"><sdk:TreeView.ItemContainerStyle><Style TargetType="sdk:TreeViewItem"><!--默认全展开--><Setter Property="IsExpanded" Value="True"/> </Style></sdk:TreeView.ItemContainerStyle></sdk:TreeView></Grid>
</UserControl>

后端cs部分:
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using BusinessObject;namespace ToolKit
{public partial class MainPage : UserControl{public MainPage(){InitializeComponent();this.Loaded += new RoutedEventHandler(MainPage_Loaded);}void MainPage_Loaded(object sender, RoutedEventArgs e){this.treeView1.ItemsSource = SampleData.SamplePlaceItemCollection;this.treeView1.ExpandAll();//var obj = this.treeView1.Items[1];}/// <summary>/// 点击节点时,选中子节点,同时设置父节点状态/// </summary>/// <param name="sender"></param>/// <param name="e"></param>private void ItemCheckbox_Click(object sender, RoutedEventArgs e){TreeViewItem item = GetParentTreeViewItem((DependencyObject)sender);if (item != null){PlaceItem feature = item.DataContext as PlaceItem;if (feature != null){UpdateChildrenCheckedState(feature);UpdateParentCheckedState(item);}}}/// <summary>/// 取得节点的父节点/// </summary>/// <param name="item"></param>/// <returns></returns>private static TreeViewItem GetParentTreeViewItem(DependencyObject item){if (item != null){DependencyObject parent = VisualTreeHelper.GetParent(item);TreeViewItem parentTreeViewItem = parent as TreeViewItem;return (parentTreeViewItem != null) ? parentTreeViewItem : GetParentTreeViewItem(parent);}return null;}/// <summary>/// 更新父节点的选中状态/// </summary>/// <param name="item"></param>private static void UpdateParentCheckedState(TreeViewItem item){//取得父节点TreeViewItem parent = GetParentTreeViewItem(item);if (parent != null){PlaceItem parentItem = parent.DataContext as PlaceItem;if (parentItem != null){                   bool? childrenCheckedState = parentItem.Children.First<PlaceItem>().IsSelected;for (int i = 1; i < parentItem.Children.Count(); i++){if (childrenCheckedState != parentItem.Children[i].IsSelected){childrenCheckedState = null;break;}}parentItem.IsSelected = childrenCheckedState;//递归处理上级父节点UpdateParentCheckedState(parent);}}}/// <summary>/// 更新子节点选中状态/// </summary>/// <param name="feature"></param>private static void UpdateChildrenCheckedState(PlaceItem feature){if (feature.IsSelected.HasValue){foreach (PlaceItem childFeature in feature.Children){childFeature.IsSelected = feature.IsSelected;if (childFeature.Children.Count() > 0){UpdateChildrenCheckedState(childFeature);}}}}}
}

可以看到了,为了处理实现全选等功能,后端还是要写一些代码处理

telerik的treeview用法:
<UserControl x:Class="Telerik.MainPage"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:common="clr-namespace:BusinessObject;assembly=BusinessObject"xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"><Grid x:Name="LayoutRoot" Background="White"><Grid.Resources><common:SampleData x:Key="SampleDataSource"></common:SampleData><!--数据节点模板--><DataTemplate x:Key="NodeTemplate"><TextBlock Text="{Binding Name}" /></DataTemplate><!--子节点模板--><telerik:HierarchicalDataTemplate x:Key="ChildTemplate" ItemTemplate="{StaticResource NodeTemplate}"ItemsSource="{Binding Children}"><TextBlock Text="{Binding Name}" /></telerik:HierarchicalDataTemplate><!--父节点模板--><telerik:HierarchicalDataTemplate x:Key="ParentTemplate" ItemTemplate="{StaticResource ChildTemplate}"ItemsSource="{Binding Children}"><TextBlock Text="{Binding Name}" /></telerik:HierarchicalDataTemplate></Grid.Resources><telerik:RadTreeViewItemsSource="{Binding Source={StaticResource SampleDataSource}, Path=SamplePlaceItemCollection}"ItemTemplate="{StaticResource ParentTemplate}" SelectionMode="Extended" IsLineEnabled="True" ItemsOptionListType="CheckList"  IsOptionElementsEnabled="True"IsRootLinesEnabled="True" IsTriStateMode="True" Margin="10"><telerik:RadTreeView.ItemContainerStyle><Style TargetType="telerik:RadTreeViewItem"><!--默认全展开--><Setter Property="IsExpanded" Value="True"/></Style></telerik:RadTreeView.ItemContainerStyle></telerik:RadTreeView></Grid>
</UserControl>

后端代码:木有!--商业控件,就是靠谱,很多功能已经帮开发者实现了.
效果:
在线演示地址:http://img.24city.com/jimmy/sl4/controls/treeview.html

示例源代码下载:http://files.cnblogs.com/yjmyzz/TreeView_Silverlight.7z

Silverlight Telerik控件学习:带CheckBox复选框的树形TreeView控件相关推荐

  1. android勾选控件_Android中CheckBox复选框控件使用方法详解

    CheckBox复选框控件使用方法,具体内容如下 一.简介 1. 2.类结构图 二.CheckBox复选框控件使用方法 这里是使用java代码在LinearLayout里面添加控件 1.新建Linea ...

  2. 纯CSS设置Checkbox复选框控件的样式

    下面是纯CSS设置Checkbox复选框控件的五种简单样式,有兴趣的可以进行改动将其变成自己想要的样式. 首先,需要添加一段CSS隐藏所有的Checkbox复选框,下面我们会改变它的外观.要做到点需要 ...

  3. Flutter Checkbox 复选框

    Flutter 复选框 有两种:一 是精简版Checkbox复选框 ,二是自带标题和副标题CheckboxListTile复选框 参数详解 属性 说明 Checkbox 复选框 value 是否选中此 ...

  4. 实战总结(二)—— CheckBox复选框和SpannableString

    CheckBox复选框和SpannableString 项目评估说登录时默认选择已阅读相关协议,应有复选框,于是做出以下改动:默认不勾选checkBox,在不勾选checkBox情况下无法登,会弹出T ...

  5. jQuery: 判断checkbox复选框是否被选中

    本文介绍如何用jQuery获取checkbox复选框选中状态,或验证是否选中或取消选中复选框. jQuery: 判断checkbox复选框是否被选中 这里我们使用jQuery .is()方法和匹配的c ...

  6. Android基础入门教程——2.3.5.RadioButton(单选按钮)Checkbox(复选框)

    Android基础入门教程--2.3.5.RadioButton(单选按钮)&Checkbox(复选框) 标签(空格分隔): Android基础入门教程 本节引言: 本节给大家带来的是Ando ...

  7. js checkbox复选框实现单选功能

    本文仅供学习交流使用,demo下载见文末 js checkbox复选框实现单选功能 <script type="text/javascript">$(":ch ...

  8. 雷林鹏分享:jQuery EasyUI 树形菜单 - 创建带复选框的树形菜单

    jQuery EasyUI 树形菜单 - 创建带复选框的树形菜单 easyui 的树(Tree)插件允许您创建一个复选框树.如果您点击一个节点的复选框,这个点击的节点信息将向上和向下继承.例如:点击 ...

  9. html 弹出复选框,js点击文本框弹出可选择的checkbox复选框

    本文分享一段代码实例,它能够点击文本框的时候,能够弹出下拉的checkbox复选框,选中复选框就能够将值写入文本框中,可能在实际应用中的效果没有这么直白简单,不过可以作为一个例子演示,以便于学习者理解 ...

最新文章

  1. 爱上MVC3系列~开发一个站点地图(俗称面包屑)
  2. python编程300集免费-python 300本电子书合集
  3. SAP UI5 web Component里的条件渲染机制
  4. 操作系统Ubuntu(实验一二)
  5. TypeScript 枚举(Enum)
  6. vue.js点击更多加载更多数据,双数组合并
  7. Android耗电优化
  8. Latex文本文档的排版
  9. spring mvc 基础学习
  10. exchange无法收发邮件_【知乎最详细】Windows邮件amp;日历UWP+QQ邮箱如何设置
  11. android页面跳转停止,android – Viewpager上的VideoView,切换页面时停止视频
  12. IntelliJ IDEA+jetty部署Eova
  13. Oracle新创建的用户没有被授权user lacks CREATE SESSION privilege logon denied
  14. Some Laws in IT
  15. 【装机】关于WINRE/ESP/LRS_ESP/MSR/PBR这些分区
  16. win10无需密码退出天擎
  17. 二元非洲秃鹫优化算法(Matlab代码实现)
  18. DC Administration Services 宣布ISDA裁决委员会2021年申请流程
  19. 中国移动5G技术概况介绍
  20. 针对e场景活动发布网站使用及产品介绍

热门文章

  1. 数字化正在使CIO职责发生变化
  2. Jupyter Notebook 添加目录
  3. fitnesse页面增加认证
  4. ecshop商城首页怎样设置广告ALT标签
  5. 批量修改table和index 的表空间
  6. 项目管理经验谈——来自项目管理群的讨论
  7. Bootstrap Paginator 分页插件的使用
  8. numpy、matplot、sklearn的安装与使用
  9. EMC首席数据治理官:“受托人”是数据湖问责的关键
  10. 2021年SDN和NFV的支出将超1580亿美元!