silverlight   发布时间:2022-05-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了有关Silverlight TreeView组件的研究[2]――Silverlight学习笔记(7)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

概述

二、带复选框的TreeView 说明:在 TreeView中设置复选框是十分常见的,这有助于我们对于同组数据的一次性选取或取消。本文就将为你介绍怎样在Silverlight中实现带有checkbox的TreeView。 ① 最初的步骤: ※ObjectCollection 这是 Silverlight Toolkit 提供的一个对象集合,用以提供静态的对象资源绑定。注意:使用时一定要添加Syste

二、带复选框的TreeView

说明:在 TreeView中设置复选框是十分常见的,这有助于我们对于同组数据的一次性选取或取消。本文就将为你介绍怎样在Silverlight中实现带有checkBox的TreeView。
最初的步骤
ObjectCollection
这是 Silverlight Toolkit 提供的一个对象集合,用以提供静态的对象资源绑定。注意:使用时一定要添加System.Windows.Controls.Toolkit的引用。在Skysigal上有一篇介绍静态资源数据绑定的好文章[链接,推荐给大家。]
HierarchicalDataTemplate
这是用于处理层次状数据而设置的数据模板,其主要用于具有 HeaderedItemsControl的组件,比如说TreeViewItem。详细@L_489_6@请参这里
INotifyPropertyChanged
向客户端发出某一属性值已更改的通知。主要用于实现数据的双向绑定。详细@L_489_6@请参 这里
实现业务对象Feature
通过实现该业务对象,将能使其与 TreeView进行交互。构建起这一对象的步骤主要有下述几步:
第一,声明可在 XAML文件显示的@L_489_6@属性添加属性标签[ContentProperty("SubComponents")]。
第二,使 Feature对象继承接口INotifyPropertyChanged。
第三,设定 Feature对象的属性
第四,添加实现 checkBox效果的重要属性HasSubcomponents和ShouldInstall。
第五,实现接口 INotifyPropertyChanged定义的函数
具体代码请见下文。
具体部署组件
在 @H_661_20@mainPage.xaml文件添加Feature对象的ObjectCollection资源,添加代表Feature对象Item的模板,以及添加有关数据对象的资源绑定。在MainPage.xaml.cs文件添加对于 TreeView组件的事件处理函数。具体代码请见下文。

实例
效果

有关Silverlight TreeView组件的研究[2]――Silverlight学习笔记(7)

有关Silverlight TreeView组件的研究[2]――Silverlight学习笔记(7)



代码
Feature 业务对象代码Feature.cs)
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Markup;
namespace SilverLightclient
{
[ ContentProperty("Subcomponents")] //声明可在 XAML文件显示的@L_489_6@属性
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));
}
}
}
}
@H_110_19@mainPage.xaml 代码
<UserControl
xmlns="http://scheR_418_11845@as.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://scheR_418_11845@as.microsoft.com/winfx/2006/xaml"
xmlns:d="http://scheR_418_11845@as.microsoft.com/expression/blend/2008" xmlns:mc="http://scheR_418_11845@as.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"
@H_661_20@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}">
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>
@H_110_19@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)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。