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:
分享到新浪Qing0
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都给我们带来了很多的惊喜,如支 ...
随机推荐
- java多线程----JUC集合”01之 框架
java集合的架构.主体内容包括Collection集合和Map类:而Collection集合又可以划分为List(队列)和Set(集合). 1. List的实现类主要有: LinkedList, A ...
- 微服务:Java EE的拯救者还是掘墓人?
有人认为,微服务的大行其道是在给Java EE下达死刑判决书.也有人认为,Java EE已死的论调可笑至极.读者朋友,你们怎么看? 引言 有人说,Java确实过于臃肿,经常“小题大做”.但PHP.No ...
- .net Core 中将原MVC中的 MvcHtmlString转换
public static IHtmlContent CustomLabelFor<TModel, TProperty>(this IHtmlHelper helper, Expressi ...
- 使用 Vue.js 结合bootstrap 实现的分页控件
原文链接:http://blog.csdn.net/qiuhaotc/article/details/53031884 源码下载: http://pan.baidu.com/s/1i4XgH6H 密码 ...
- android 通过webview调起支付宝app支付
网站学习:http://blog.csdn.net/zhuyu19911016520/article/details/71763900
- Educational Codeforces Round 57 (Rated for Div. 2)
我好菜啊. A - Find Divisible 好像没什么可说的. #include<cstdio> #include<cstring> #include<algori ...
- BZOJ2724 [Violet]蒲公英 分块
题目描述 经典区间众数题目 然而是权限题,所以题目链接放Luogu的 题解 因为太菜所以只会$O(n*\sqrt{n}+n*\sqrt{n}*log(n))$的做法 就是那种要用二分的,并不会clj那 ...
- redis持久化RDB和AOF-转载
Redis 持久化: 提供了多种不同级别的持久化方式:一种是RDB,另一种是AOF. RDB 持久化可以在指定的时间间隔内生成数据集的时间点快照(point-in-time snapshot). AO ...
- LA 4287 等价性证明(强连通分量缩点)
https://vjudge.net/problem/UVALive-4287 题意: 给出n个结点m条边的有向图,要求加尽量少的边,使得新图强连通. 思路:强连通分量缩点,然后统计缩点后的图的每个结 ...
- LA 3027 合作网络
https://vjudge.net/problem/UVALive-3027 题意: 有n个结点,初始时每个结点的父节点都不存在.你的任务是执行一次I操作和E操作,格式如下: I u v:把结点u的 ...