接下来是缩进,没有缩进的Tree怎么看都不顺眼。

首先,定义节点深度Depth(注:回叫方法暂没有代码,以后要用到):

 1: /// <summary> 
 2: /// Using a DependencyProperty as the backing store for Depth. This enables animation, styling, binding, etc... 
 3: /// </summary> 
 4: public static readonly DependencyProperty DepthProperty =
 5:  DependencyProperty.Register("Depth", typeof(int), typeof(FancyTreeViewItem),
 6:  new PropertyMetadata(0, new PropertyChangedCallback(FancyTreeViewItem.OnDepthPropertyChanged))
 7: );
 8: 
 1: /// <summary> 
 2: /// Gets or sets the node depth level in tree 
 3: /// </summary> 
 4: public int Depth
 5: {
 6:  get { return (int)GetValue(DepthProperty); }
 7:  set { SetValue(DepthProperty, value); }
 8: }
 9:  
 1: /// <summary> 
 2: /// Call back when Depth 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 OnDepthPropertyChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
 7: {
 8:  
 9: }
 10:  

接下来就是写出计算节点在树中深度值的方法:

 1: /// <summary> 
 2: /// For getting the item depth level 
 3: /// </summary> 
 4: /// <returns>The result depth level</returns> 
 5: private int GetDepthLevel()
 6: {
 7:  int depthLevel = 0;
 8:  FrameworkElement element = this;
 9:  
 10:  while (element.Parent != null)
 11: {
 12:  var parent = element.Parent as FancyTreeViewItem;
 13:  
 14:  if (parent != null)
 15: {
 16: depthLevel++;
 17:  ////depthLevel = parent.GetDepthLevel() + 1; 
 18: }
 19:  
 20: element = element.Parent as FrameworkElement;
 21: }
 22:  
 23:  return depthLevel;
 24: }
 25:  

绑定样式的时候把缩进量放进去,在public override void OnApplyTemplate()中添加:

 1: if (this.Indent != null)
 2: {
 3:  this.Indent.Width = this.GetDepthLevel() * 20;
 4: }
 5:  

最后把Boder的边框颜色去掉,运行看效果:

图4.1 带缩进的效果图

最新文章

  1. 在 ASP.NET CORE 中使用 SESSION
  2. Go语言 数组
  3. JavaScript中字符串去掉特殊字符和转义字符
  4. java项目中读取properties文件
  5. 利用zabbix监控某个目录大小
  6. 1、C到C++安全性增强
  7. 9个offer,12家公司,35场面试,从微软到谷歌,应届计算机毕业生的2012求职之路
  8. Javascript类型&mdash;&mdash;boolean类型
  9. RDIFramework.NET(.NET快速信息化系统开发框架) Web版介绍
  10. Vuejs——v-on
  11. Arch Linux安装记录
  12. hdu 2553 N皇后
  13. SQL Server存储过程邮件发送以表格方式发送
  14. 最优的路线(floyd最小环)
  15. 英语口语练习系列-C06-购物
  16. C#简单画图Draw研究学习
  17. Tomcat Jboss Apache WebLogic区别与比较(阿里面试)
  18. Docker运行操作系统环境(BusyBox&amp;Alpine&amp;Debian/Ubuntu&amp;CentOS/Fedora)
  19. 理解JavaScript对象
  20. Leecode刷题之旅-C语言/python-217存在重复元素

热门文章

  1. Object-C-属性参数
  2. python yield yield from
  3. Kettle 学习导航帖整理
  4. linux常用命令:chown 命令
  5. PHP empty、isset、isnull的区别
  6. mysql 从数据库中获取多条记录,二维展示数据
  7. Winform 出现“Win已停止工作”解决方法
  8. UVA302 John&#39;s trip(欧拉回路)
  9. 02: shell中的if、case、for等语句
  10. Node-webkit 安装使用npm安装模块方法