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. python 同时迭代多个序列

    每次分别从一个序列中取一个元素 >>> xpts = [1, 5, 4, 2, 10, 7] >>> ypts = [101, 78, 37, 15, 62, 99 ...

  2. Applying the Kappa architecture in the telco industry

    https://www.oreilly.com/ideas/applying-the-kappa-architecture-in-the-telco-industry Kappa architectu ...

  3. js dom 操作技巧

    1.创建元素 创建元素:document.createElement() 使用document.createElement()可以创建新元素.这个方法只接受一个参数,即要创建元素的标签名.这个标签名在 ...

  4. Linux服务器配置---ftp配置

    FTP其他配置 在配置文件中,还有一些关于vsftpd的其他设置,这里列出来 # Example config file /etc/vsftpd/vsftpd.conf # Allow anonymo ...

  5. php ci 报错 Object not found! The requested URL was not found on this server. If you entered the URL manually please check

    Object not found! The requested URL was not found on this server. The link on the referring page see ...

  6. DeepMind已将AlphaGo引入多领域 Al泡沫严重

    DeepMind已将AlphaGo引入多领域 Al泡沫严重 在稳操胜券的前提下,谷歌旗下的AlphaGo还是向柯洁下了战书.4月10日,由中国围棋协会.浙江省体育局.谷歌三方联合宣布,将于5月23日至 ...

  7. 使用Astah画UML类图经验总结

    从学习需求工程与UML开始,就开始接触到Astah这款软件,但是当时完全是为了对UML各种图的了解加深才使用了这款软件.当时画图,都是完全凭借自己想,并没有考虑实际情况,而且画的图都是很简单的,甚至有 ...

  8. Linux中编写Bash脚本的10个技巧

    Shell 脚本编程 是你在 Linux 下学习或练习编程的最简单的方式.尤其对 系统管理员要处理着自动化任务,且要开发新的简单的实用程序或工具等(这里只是仅举几例)更是必备技能. 本文中,我们将分享 ...

  9. 深入理解Java虚拟机 #01# 自己编译JDK

    x 首先用书上的脚本尝试,失败. 之后根据源文件的 README 编译,抛出: root@linux:/opt/openjdk# sh ./get_source.sh ERROR: Need init ...

  10. PHP开发者成长图

    作为PHP开发者,根据自己给自己的定位和这幅图,是否觉得自己还需要比平时更努力呢~