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

Silverlight自定义控件系列 – TreeView (4) 缩进,第1张

概述  http://blog.sina.com.cn/s/blog_7019da0c0100lpwv.html 接下来是缩进,没有缩进的Tree怎么看都不顺眼。 首先,定义节点深度Depth(注:回叫方法暂没有代码,以后要用到): 1: /// <summary> 2: /// Using a DependencyProperty as the backing store for Depth.   @H_301_6@http://blog.sina.com.cn/s/blog_7019da0c0100lpwv.html

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

@H_301_6@首先,定义节点深度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:  
@H_301_6@接下来就是写出计算节点在树中深度值的方法:

 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:  
@H_301_6@绑定样式的时候把缩进量放进去,在public overrIDe voID OnApplyTemplate()中添加:

 1: if (this.Indent != null)
 2: {
 3:  this.Indent.WIDth = this.GetDepthLevel() * 20;
 4: }
 5:  
@H_301_6@最后把Boder的边框颜色去掉,运行看效果:

图4.1 带缩进的效果图

@H_131_404@ 总结

以上是内存溢出为你收集整理的Silverlight自定义控件系列 – TreeView (4) 缩进全部内容,希望文章能够帮你解决Silverlight自定义控件系列 – TreeView (4) 缩进所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1070433.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-26
下一篇 2022-05-26

发表评论

登录后才能评论

评论列表(0条)

保存