![有关Silverlight TreeView组件的研究[2]――Silverlight学习笔记(7),第1张 有关Silverlight TreeView组件的研究[2]――Silverlight学习笔记(7),第1张](/aiimages/%E6%9C%89%E5%85%B3Silverlight+TreeView%E7%BB%84%E4%BB%B6%E7%9A%84%E7%A0%94%E7%A9%B6%5B2%5D%E2%80%95%E2%80%95Silverlight%E5%AD%A6%E4%B9%A0%E7%AC%94%E8%AE%B0%287%29.png)
二、带复选框的TreeVIEw
说明:在 TreeVIEw中设置复选框是十分常见的,这有助于我们对于同组数据的一次性选取或取消。本文就将为你介绍怎样在Silverlight中实现带有CheckBox的TreeVIEw。 ① 最初的步骤: ※ObjectCollection 这是 Silverlight Toolkit 提供的一个对象集合,用以提供静态的对象资源绑定。注意:使用时一定要添加System.windows.Controls.Toolkit的引用。在Skysigal上有一篇介绍静态资源数据绑定的好文章[链接,推荐给大家。] ※HIErarchicalDataTemplate 这是用于处理层次状数据而设置的数据模板,其主要用于具有 headeredItemsControl的组件,比如说TreeVIEwItem。详细内容请参考这里。 ※INotifyPropertyChanged 向客户端发出某一属性值已更改的通知。主要用于实现数据的双向绑定。详细内容请参考 这里。 ② 实现业务对象Feature: 通过实现该业务对象,将能使其与 TreeVIEw进行交互。构建起这一对象的步骤主要有下述几步: 第一,声明可在 XAML文件中显示的内容属性,添加属性标签[contentproperty("SubComponents")]。 第二,使 Feature对象继承接口INotifyPropertyChanged。 第三,设定 Feature对象的属性。 第四,添加实现 CheckBox效果的重要属性HasSubcomponents和ShouldInstall。 第五,实现接口 INotifyPropertyChanged定义的函数。 具体代码请见下文。 ③ 具体部署组件: 在 MainPage.xaml文件中添加Feature对象的ObjectCollection资源,添加代表Feature对象Item的模板,以及添加有关数据对象的资源绑定。在MainPage.xaml.cs文件中添加对于 TreeVIEw组件的事件处理函数。具体代码请见下文。 @H_502_96@ 实例: 效果图: @H_502_96@@H_502_96@@H_502_96@ 代码段: Feature 业务对象代码(Feature.cs): using System.Collections.ObjectModel; using System.ComponentModel; using System.windows.Markup; namespace SilverlightClIEnt { [ contentproperty("Subcomponents")] //声明可在 XAML文件中显示的内容属性 public class Feature : INotifyPropertyChanged //继承接口 INotifyPropertyChanged用于双向数据绑定 { //Feature对象的属性 public string Featurename { get; set; } public string Description { get; set; } //声明全局变量 public Collection<Feature> Subcomponents { get; private set; } private bool? _shouldInstall; //是否有子组件 public bool HasSubcomponents { get { return Subcomponents.Count > 0; } } //是否允许 Feature进行安置 public bool? ShouldInstall { get { return _shouldInstall; } set { if (value != _shouldInstall) { _shouldInstall = value; OnPropertyChanged( "ShouldInstall"); } } } //构造函数 public Feature() { Subcomponents = new Collection<Feature>(); ShouldInstall = true; } //事件委托 public event PropertyChangedEventHandler PropertyChanged; //实现接口 INotifyPropertyChanged定义函数 private voID OnPropertyChanged(string propertyname) { PropertyChangedEventHandler handler = PropertyChanged; if (null != handler) { handler.Invoke( this,new PropertyChangedEventArgs(propertyname)); } } } } MainPage.xaml 代码: <UserControl 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:toolkit="clr-namespace:System.windows.Controls;assembly=System.windows.Controls.Toolkit" xmlns:controls="clr-namespace:System.windows.Controls;assembly=System.windows.Controls" xmlns:common="clr-namespace:System.windows;assembly=System.windows.Controls" xmlns:samplesCommon="clr-namespace:SilverlightClIEnt" mc:Ignorable="d" x:Class="SilverlightClIEnt.MainPage" WIDth="640" Height="480"> <GrID x:name="LayoutRoot" Background="White" WIDth="640" Height="480"> <StackPanel> <StackPanel.Resources> <!-- 用于安置的示例 Features --> <toolkit:ObjectCollection x:Key="CorporationFeatures"> <samplesCommon:Feature Featurename="公司部门 " Description="公司各部门的结构 "> <samplesCommon:Feature Featurename="建筑部 " Description="负责公司的工程项目 "> <samplesCommon:Feature Featurename="设计科 " Description="负责项目的设计 " /> <samplesCommon:Feature Featurename="工程科 " Description="负责项目的具体实施 " /> </samplesCommon:Feature> <samplesCommon:Feature Featurename="管理部 " Description="负责管理公司的财务与人事 "> <samplesCommon:Feature Featurename="财务科 " Description="负责公司的对内对外的财务事宜 " /> <samplesCommon:Feature Featurename="总务人事科 " Description="负责公司日常事务及员工招聘 " /> </samplesCommon:Feature> </samplesCommon:Feature> </toolkit:ObjectCollection> <!-- 代表一个 Feature项的模板 --> <common:HIErarchicalDataTemplate x:Key="NodeTemplate" ItemsSource="{Binding Subcomponents}"> <StackPanel OrIEntation="Horizontal" tooltipService.tooltip="{Binding Description}"> <CheckBox IsTabStop="False" IsThreeState="{Binding HasSubcomponents}" IsChecked="{Binding ShouldInstall,Mode=TwoWay}" Click="ItemCheckBox_Click" /> <ContentPresenter Content="{Binding Featurename}" /> </StackPanel> </common:HIErarchicalDataTemplate> </StackPanel.Resources> <GrID> <GrID.ColumnDeFinitions> <ColumnDeFinition WIDth="*" /> <ColumnDeFinition WIDth="2*" /> </GrID.ColumnDeFinitions> <controls:TreeVIEw GrID.Column="0" ItemTemplate="{StaticResource NodeTemplate}" ItemsSource="{StaticResource CorporationFeatures}" FontSize="14"> <!-- 用来一次展开 TreeVIEw所有结点 --> <controls:TreeVIEw.ItemContainerStyle> <Style targettype="controls:TreeVIEwItem"> <Setter Property="IsExpanded" Value="True" /> </Style> </controls:TreeVIEw.ItemContainerStyle> </controls:TreeVIEw> </GrID> </StackPanel> </GrID> </UserControl> MainPage.xaml.cs 代码: 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; namespace SilverlightClIEnt { public partial class MainPage : UserControl { public MainPage() { InitializeComponent(); } //处理 CheckBox点击事件 private voID ItemCheckBox_Click(object sender,RoutedEventArgs e) { TreeVIEwItem item = GetParentTreeVIEwItem((DependencyObject)sender); if (item != null) { Feature feature = item.DataContext as Feature; if (feature != null) { UpdateChildrenCheckedState(feature); //更新子组件选中状态 UpdateParentCheckedState(item); //更新父组件选中状态 } } } //静态方法:获取父级 TreeVIEwItem 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);//如果父级 TreeVIEwItem存在则返回,否则就递归寻找 } //找不到父对象,返回父对象不存在 return null; } //静态方法:更新父级 TreeVIEwItem选中状态 private static voID UpdateParentCheckedState(TreeVIEwItem item) { TreeVIEwItem parent = GetParentTreeVIEwItem(item);//获取父级 TreeVIEwItem if (parent != null)//如果父对象不为空,为空则退出递归寻找 { Feature feature = parent.DataContext as Feature;//对象转换 if (feature != null)//如果对象不为空 { //更新子组件的选中状态 bool? childrenCheckedState = feature.Subcomponents.First<Feature>().ShouldInstall;//得到第一个子组件的选中状态 for (int i = 1; i < feature.Subcomponents.Count(); i++) { if (childrenCheckedState != feature.Subcomponents[i].ShouldInstall) { childrenCheckedState = null; break; } } //将父组件的选中状态与子组件置为相同 feature.ShouldInstall = childrenCheckedState; //继续递归搜索 . UpdateParentCheckedState(parent); } } } //用递归更新子组件的选中状态 private static voID UpdateChildrenCheckedState(Feature feature) { if (feature.ShouldInstall.HasValue) { foreach (Feature childFeature in feature.Subcomponents) { childFeature.ShouldInstall = feature.ShouldInstall; if (childFeature.Subcomponents.Count() > 0) { UpdateChildrenCheckedState(childFeature); } } } } }
http://www.cnblogs.com/Kinglee/archive/2009/08/11/1543794.html
总结以上是内存溢出为你收集整理的有关Silverlight TreeView组件的研究[2]――Silverlight学习笔记(7)全部内容,希望文章能够帮你解决有关Silverlight TreeView组件的研究[2]――Silverlight学习笔记(7)所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)