二、带复选框的TreeView
说明:在
TreeView中设置复选框是十分常见的,这有助于我们对于同组数据的一次性选取或取消。本文就将为你介绍怎样在Silverlight中实现带有checkBox的TreeView。
①
最初的步骤:
这是
Silverlight Toolkit 提供的一个对象集合,用以提供静态的对象资源绑定。注意:使用时一定要添加System.Windows.Controls.Toolkit的引用。在Skysigal上有一篇介绍静态资源数据绑定的好文章[链接,推荐给大家。]
※HierarchicalDataTemplate
这是用于处理层次状数据而设置的数据模板,其主要用于具有
HeaderedItemsControl的组件,比如说TreeViewItem。详细@L_489_6@请参考这里。
※INotifyPropertyChanged
②
实现业务对象Feature:
通过实现该业务对象,将能使其与
TreeView进行交互。构建起这一对象的步骤主要有下述几步:
第二,使
Feature对象继承接口INotifyPropertyChanged。
第五,实现接口
INotifyPropertyChanged定义的函数。
③
具体部署组件:
在
@H_661_20
@mainPage.xaml
文件中
添加Feature对象的Ob
jectCollection资源,
添加代表Feature对象Item的模板,以及
添加有关数据对象的资源绑定。在MainPage.xam
l.cs
文件中
添加对于
TreeView组件的事件处理函数。具体代码请见下文。
实例:
Feature
业务对象代码(Feature.cs):
using Sy
stem.ComponentModel;
[
ContentProperty("Subcomponents")] //声明可在
XAML文件中显示的@L_489_6@属性
public class Feature : INotifyPropertyChanged //继承接口
INotifyPropertyChanged用于双向数据绑定
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;
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="Silver
Lightclient.MainPage"
Width="640" Height="480">
<Grid x:Name="LayoutRoot"
BACkground="White" Width="640" Height="480">
<StackPanel>
<!-- 用于安置的示例
Features -->
<toolkit:Ob
jectCollection x:Key="CorporationFeatures">
<samplesCommon:Feature Featur
ename="公司部门
" Description="公司各部门的结构
">
<samplesCommon:Feature Featur
ename="建筑部
" Description="负责公司的工程项目
">
<samplesCommon:Feature Featur
ename="设计科
" Description="负责项目的设计
" />
<samplesCommon:Feature Featur
ename="工程科
" Description="负责项目的具体实施
" />
</samplesCommon:Feature>
<samplesCommon:Feature Featur
ename="管理部
" Description="负责管理公司的财务与人事
">
<samplesCommon:Feature Featur
ename="财务科
" Description="负责公司的对内对外的财务事宜
" />
<samplesCommon:Feature Featur
ename="总务人事科
" Description="负责公司日常事务及员工招聘
" />
</samplesCommon:Feature>
</samplesCommon:Feature>
</toolkit:Ob
jectCollection>
<!-- 代表
一个
Feature项的模板 -->
<common:HierarchicalDataTemplate x:Key="NodeTemplate" Items
source="
{Binding Subcomponents}">
<StackPanel Orientation="Horizontal" ToolTip
service.ToolTip="
{Binding Description}">
IsThreeState="{Binding HasSubcomponents}"
Ischecked="{Binding ShouldInstall,Mode=TwoWay}"
/>
<ContentPresenter Content="
{Binding Featur
ename}" />
</StackPanel>
</common:HierarchicalDataTemplate>
<Grid>
<controls:TreeView
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.xam
l.cs
代码:
using Sy
stem.Window
s.Controls;
using Sy
stem.Window
s.Documents;
using Sy
stem.Window
s.Media.Animation;
public partial class MainPage : UserControl
public MainPage()
}
private void ItemcheckBox_Click(object sender,RoutedEventArgs E)
TreeViewItem item = GetParentTreeViewItem((DependencyObject)sender);
Feature feature = item.DataContext as Feature;
}
}
}
private static TreeViewItem GetParentTreeViewItem(DependencyObject item)
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)
children
checkedState =
null;
break;
}
}
//将父组件的选中状态与子组件置为相同
feature.ShouldInstall = children
checkedState;
}
}
}
//用递归更新子组件的选中状态
private static void updateChildrencheckedState(Feature featurE)
foreach (Feature childFeature in feature.Subcomponents)
childFeature.ShouldInstall = feature.ShouldInstall;
if (childFeature.Subcomponents.Count() > 0)
}
}
}
}
}
http://www.cnblogs.com/Kinglee/archive/2009/08/11/1543794.html