Silverlight自定义控件系列 – TreeView (4) 缩进
2013-09-03 19:16
134 查看
接下来是缩进,没有缩进的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 带缩进的效果图
相关文章推荐
- Silverlight自定义控件系列 – TreeView (3) 添加展开和收起事件
- Silverlight自定义控件系列 – TreeView (2) 基本布局和States
- Silverlight自定义控件系列 – TreeView (1)
- Silverlight自定义控件系列 – TreeView (2) 基本布局和States
- Silverlight自定义控件系列 – TreeView (3) 添加展开和收起事件
- Silverlight自定义控件系列 – TreeView (4) 缩进
- silverlight系列(DataGrid、DataPager、TreeView、MediaElement)
- silverlight系列(DataGrid、DataPager、TreeView、MediaElement)
- 转:[Silverlight入门系列]使用MVVM模式(9): 想在ViewModel中控制TreeView节点展开?
- Silverlight实用窍门系列:7.制作可拖动的自定义控件,获取拖拽后控件坐标【实例源码下载】
- [Silverlight入门系列]Prism中TreeView真正实现MVVM模式和Expanded发生时异步动态加载子节点(WCFRiaService)
- Silverlight 自定义控件的继承问题
- C#开发WPF/Silverlight动画及游戏系列教程(Game Tutorial):(二十八) 经典式属性设计及完美的物理攻击系统
- 一步一步学Silverlight 2系列(8):使用样式封装控件观感
- 一步一步学Silverlight 2系列(24):与浏览器交互相关辅助方法
- Silverlight 5 RC新特性探索系列:12.Silverlight 5 RC 窗口模式下访问自定义DLL和WIN32 API
- Android自定义控件系列四:自定义开关按钮(三)--- 自定义属性
- 稳扎稳打Silverlight(36) - 3.0控件之TreeView, ListBox增强, DataGrid增强, MediaElement增强
- silverlight系列(AnimationView、KeyFrameAnimation)
- Mvvm Light Toolkit for wpf/silverlight系列之Messenger