Silverlight自定义控件系列 – TreeView (2) 基本布局和States
TreeView的树形结构都以缩进方式显示,现在来完成这部分。
首先,要定义出每个节点上都包含什么东西。先看看Win7资源管理器的TreeView:
图2.1 资源管理器
一个通用的TreeView至少有缩进,箭头,Header。但是我们常常会用到勾选功能,也会用到图标来达到更高的友好度,因此这里暂时先按一下的格局来定义:
缩进 | 箭头 | 选择框 | 图标 | 显示文字 |
子节点 |
那么开始了。用上次的模板,把Root分成两行,在Root的第一行中添加一个Border,用来放置节点的所有物件;在第二行中添加一个ItemsPresenter,名为ItemsHost,用来表示子节点集合。
接着在Border中再添加一个Grid用来布局,Grid分5列(原因看上表格),第一列添加一个Grid名为Indention,缩进用;第二列添加一个ToggleButton名为Expander作箭头按钮(非常好用);第三列放CheckBox;第四列放一个Image;把Header拖到第五列上。这样,简单的基础布局做好了。
图2.2.1 基础布局(1)
图2.2.2 基础布局(2)
图2.3 基础布局效果图
现在我们开始实现通过点击Expander来显示和隐藏子节点。首先增加展开和收起的模板状态:
1: [TemplateVisualState(Name = "Expanded", GroupName = "ExpandedStates")]
2: [TemplateVisualState(Name = "Collapsed", GroupName = "ExpandedStates")]
3: public class FancyTreeViewItem : HeaderedItemsControl
4:
编译之后,在Blend 4的States界面上会看到:
图2.4 States中的新状态
现在我们在Base下把ItemsHost的Visibility设为Collapsed,然后点选Expanded,当左边圆点变红后,把ItemsHost的Visibility设为Visible。
图2.5 录制不同的States
States已经录制好了,但是现在点击Expander还不能转到相应的State,所以要添加一个状态变量用来记录目前是被展开还是收起状态,当状态改变的时候转到相应的State。
记录状态的IsExpanded以及回叫方法OnIsExpandedPropertyChanged:
1: /// <summary>
2: /// Using a DependencyProperty as the backing store for IsExpanded. This enables animation, styling, binding, etc...
3: /// </summary>
4: public static readonly DependencyProperty IsExpandedProperty =
5: DependencyProperty.Register("IsExpanded", typeof(bool), typeof(FancyTreeViewItem),
6: new PropertyMetadata(false, new PropertyChangedCallback(FancyTreeViewItem.OnIsExpandedPropertyChanged))
7: );
8:
1: /// <summary>
2: /// Call back when IsExpanded property has been changed
3: /// </summary>
4: /// <param name="o">The target object</param>
5: /// <param name="e">The property changed event arrguments</param>
6: private static void OnIsExpandedPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
7: {
8:
9: }
10:
1: #region Properties
2:
3: /// <summary>
4: /// Gets or sets a value indicating whether the items have been expanded
5: /// </summary>
6: public bool IsExpanded
7: {
8: get { return (bool)GetValue(IsExpandedProperty); }
9: set { SetValue(IsExpandedProperty, value); }
10: }
11:
12: #endregion
13:
转向状态的方法和事件的触发:
1: /// <summary>
2: /// To raise the event handler
3: /// </summary>
4: /// <param name="handler">The target event handler</param>
5: /// <param name="e">The routed event arrguments</param>
6: private void RaiseEvent(RoutedEventHandler handler, RoutedEventArgs e)
7: {
8: if (handler != null)
9: {
10: handler(this, e);
11: }
12: }
13:
14: /// <summary>
15: /// To update the control's visual state
16: /// </summary>
17: /// <param name="userTransitions">The flag that whether allow to update</param>
18: internal virtual void UpdateVisualState(bool userTransitions)
19: {
20: if (this.IsExpanded)
21: {
22: VisualStateManager.GoToState(this, "Expanded", userTransitions);
23: }
24: else
25: {
26: VisualStateManager.GoToState(this, "Collapsed", userTransitions);
27: }
28: }
29:






0
Silverlight自定义控件系列 – TreeView (2) 基本布局和States的更多相关文章
- Silverlight自定义控件系列 – TreeView (1)
原文路径:http://blog.csdn.net/wlanye/article/details/7265457 很多人都对MS自带的控件不太满意(虽然MS走的是简约风格),都会试图去修改或创建让 ...
- Silverlight自定义控件系列 – TreeView (4) 缩进
接下来是缩进,没有缩进的Tree怎么看都不顺眼. 首先,定义节点深度Depth(注:回叫方法暂没有代码,以后要用到): 1: /// <summary> 2: /// Using a De ...
- Silverlight自定义控件系列 – TreeView (3) 添加展开和收起事件
由于Writer嫌我文章过长,只能把上篇拆开两半了.以下是接着上篇的. 准备工作做完了,现在就要完成点击事件. 定义Expander和单击事件: 1: /// <summary> 2: / ...
- 一步一步学Silverlight 2系列(3):界面布局
述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- 一步一步学Silverlight 2系列文章
概述 由TerryLee编写的<Silverlight 2完美征程>一书,已经上市,在该系列文章的基础上补充了大量的内容,敬请关注.官方网站:http://www.dotneteye.cn ...
- 一步一步学Silverlight 2系列(22):在Silverlight中如何用JavaScript调用.NET代码
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- 一步一步学Silverlight 2系列(20):如何在Silverlight中与HTML DOM交互(下)
述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- 一步一步学Silverlight 2系列(19):如何在Silverlight中与HTML DOM交互(上)
概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...
- 一步一步学Silverlight 2系列(18):综合实例之RSS阅读器
一步一步学Silverlight 2系列(18):综合实例之RSS阅读器 概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支 ...
随机推荐
- Groovy中的闭包
Closures(闭包) 本节主要讲groovy中的一个核心语法:closurs,也叫闭包.闭包在groovy中是一个处于代码上下文中的开放的,匿名代码块.它可以访问到其外部的变量或方法. 1. 句法 ...
- python webdriver firefox 登录126邮箱,先添加联系人,然后进入首页发送邮件,带附件。
代码:#encoding=utf-8from selenium import webdriverfrom selenium.webdriver.common.keys import Keysfrom ...
- eclispe 相关设置
1. 关闭js文件校验: 1). windows->preference->Java Script->Validator->Errors/Warnings->Enable ...
- mysql删除有外链索引数据,Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法
mysql删除有外链索引数据Cannot delete or update a parent row: a foreign key constraint fails 问题的解决办法查询:DELETE ...
- web前端----html表单操作
form表单 功能:表单用于向服务器传输数据,从而实现用户与Web服务器的交互 表单能够包含input系列标签,比如文本字段.复选框.单选框.提交按钮等等. 表单还可以包含textarea.selec ...
- vc编辑器常用设置
代码格式化 1.选中代码: 2.ctrl+K: 3.ctrl+F; 显示行号
- Android 实践项目开发二
在地图开发中项目中,我这周主要完成的任务是和遇到的问题是以下几个方面. 1.在本次的项目中主要是利用百度地图的.jar包实现地图的定位与搜索功能,需要在百度地图开发中心网站取得 密钥,并下载相关.ja ...
- poj Meteor Shower - 搜索
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 16313 Accepted: 4291 Description Bess ...
- Package libvirt was not found in the pkg-config search path
关于pip安装libvirt-python的时候提示Package libvirt was not found in the pkg-config search path的问题解决方法 1.一开始以为 ...
- IT人士级别的划分
IT领袖:年入过亿(例如任正非.马化腾.李彦宏.丁磊.马云等,包括期权股票以及投资理财等收入.) IT大哥:年入千万(级别次于以上几位大佬的公司老板,不缺钱,普遍对上一条里的人物羡慕嫉妒恨.) IT精 ...