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:  
分享 分享到新浪Qing

0

Silverlight自定义控件系列 – TreeView (2) 基本布局和States的更多相关文章

  1. Silverlight自定义控件系列 – TreeView (1)

      原文路径:http://blog.csdn.net/wlanye/article/details/7265457 很多人都对MS自带的控件不太满意(虽然MS走的是简约风格),都会试图去修改或创建让 ...

  2. Silverlight自定义控件系列 – TreeView (4) 缩进

    接下来是缩进,没有缩进的Tree怎么看都不顺眼. 首先,定义节点深度Depth(注:回叫方法暂没有代码,以后要用到): 1: /// <summary> 2: /// Using a De ...

  3. Silverlight自定义控件系列 – TreeView (3) 添加展开和收起事件

    由于Writer嫌我文章过长,只能把上篇拆开两半了.以下是接着上篇的. 准备工作做完了,现在就要完成点击事件. 定义Expander和单击事件: 1: /// <summary> 2: / ...

  4. 一步一步学Silverlight 2系列(3):界面布局

    述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  5. 一步一步学Silverlight 2系列文章

    概述 由TerryLee编写的<Silverlight 2完美征程>一书,已经上市,在该系列文章的基础上补充了大量的内容,敬请关注.官方网站:http://www.dotneteye.cn ...

  6. 一步一步学Silverlight 2系列(22):在Silverlight中如何用JavaScript调用.NET代码

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  7. 一步一步学Silverlight 2系列(20):如何在Silverlight中与HTML DOM交互(下)

    述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  8. 一步一步学Silverlight 2系列(19):如何在Silverlight中与HTML DOM交互(上)

    概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支持框架语言Visual Basic, Visual C#, IronRuby, ...

  9. 一步一步学Silverlight 2系列(18):综合实例之RSS阅读器

    一步一步学Silverlight 2系列(18):综合实例之RSS阅读器   概述 Silverlight 2 Beta 1版本发布了,无论从Runtime还是Tools都给我们带来了很多的惊喜,如支 ...

随机推荐

  1. animation-fill-mode

    animation-fill-mode: none:默认值.不设置对象动画之外的状态 forwards:结束后保持动画结束时的状态,但当animation-direction为0,则动画不执行,持续保 ...

  2. Python: 字符串搜索和匹配,re.compile() 编译正则表达式字符串,然后使用match() , findall() 或者finditer() 等方法

    1. 使用find()方法 >>> text = 'yeah, but no, but yeah, but no, but yeah' >>> text.find( ...

  3. python选择排序算法总结

    选择排序算法: a=[6,5,4,3,2,1] 算法思路: 第一步:在列表的第一个位置存放此队列的最小值 声明一个变量min_index等于列表的第一个坐标值0 从第一个位置0坐标开始,和它后边所有的 ...

  4. Python 除法运算

    Python中的除法较其它语言显得非常高端,有套很复杂的规则.Python中的除法有两个运算符,/和// 首先来说/除法: 在python 2.x中/除法就跟我们熟悉的大多数语言,比如Java啊C啊差 ...

  5. python网络编程之一

    套接字的详细介绍会在另一篇博文指出,此片博文主要是python套接字的简单客户端编写. 两种套接字介绍: 面向连接的套接字:面向连接的套接字提供序列化,可靠的和不重复的数据交付.面向连接是可靠的传输, ...

  6. Python3 Selenium自动化-select下拉框

    Python3 Selenium自动化-select下拉框 selenium介绍select下拉框相关的操作方法:

  7. xargs 原理&使用

    1. 简介 之所以能用到这个命令,是由于很多 linux 命令不支持用管道传递参数,例如 find /sbin -perm +700 | ls -l 这个命令是错误的 find /sbin -perm ...

  8. troubleshooting-sqoop mysql导入hive 报:GC overhead limit exceeded

    Halting due to Out Of Memory Error...18/09/13 21:42:17 INFO mapreduce.Job: Task Id : attempt_1536756 ...

  9. 20145322 Exp5 MS11_050

    20145322 Exp5 MS11_050 实验过程 msfconsole命令进入控制台 使用命令search ms11_050查看针对MS11_050漏洞的攻击模块 使用命令 use exploi ...

  10. 小K(wifi)插座剖解

    1.主控 AR9331 400MHZ MIPS 24k内核 2.flash:w9425G6JH-5 1352P 6316CF500ZY  RAM 32M