WPF⼀个完整的TreeView使⽤实例:(⼀)⾃定义控件样式
+数据源绑定+动态添加⽗⼦节点
TreeView控件可在树结构中显⽰分层数据,其中的项可以展开和折叠。它可以包含多种类型的控件,如Button、Lable、Image等控
件,可以通过绑定到数据源并使⽤HieratchicalDataTemplate对象来填充其树。可以修改默认ControlTemplate以使控件具有独特的外
观。
这⾥举⼀个完整的TreeView实例来进⾏说明。本节主要实现样式+数据源绑定+动态添加⽗⼦节点的功能。效果如下:
1、主窗体中加⼊TreeView控件
1 <TreeView x:Name="treeView" Background="Transparent" MinHeight="280" Width="200"
2 ScrollViewer.HorizontalScrollBarVisibility="Hidden" ScrollViewer.VerticalScrollBarVisibility="Auto"
3 MouseDoubleClick="TreeView_MouseDoubleClick">
4 <TreeView.ItemTemplate>
5 <HierarchicalDataTemplate ItemsSource="{Binding Path=ChildNodes}">
6 <TextBlock x:Name="showName" HorizontalAlignment="Left" VerticalAlignment="Center" Margin="0,8,0,0" Height="26"
7 FontSize="{Binding Path=SetFontSize}" FontWeight="{Binding Path=SetFontWeight}" Text="{Binding Path=NodeName, Mode=TwoWay}"
8 </HierarchicalDataTemplate>
9 </TreeView.ItemTemplate>
10 </TreeView>
fontweight属性bold由于我们的这个TreeView控件⽗⼦节点字体样式、背景颜⾊等不同,所以使⽤Binding的⽅式通过后台进⾏设置。
2、TreeView的Model:TreeViewNode.cs
1 public class TreeViewNode : INotifyPropertyChanged
2 {
3 public event PropertyChangedEventHandler PropertyChanged;
4 protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
5 {
6 PropertyChangedEventHandler handler = PropertyChanged;
7 if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
8 }
9
10 #region 属性字段
11 private int id;
12 /// <summary>
13 /// 节点ID
14 /// </summary>
15 public int Id
16 {
17 get
18 {
19 return id;
20 }
21 set
22 {
23 id = value;
24 }
25 }
26
27 private int parentId;
28 /// <summary>
29 /// ⽗节点ID
30 /// </summary>
31 public int ParentId
32 {
33 get
34 {
35 return parentId;
36 }
37 set
38 {
39 parentId = value;
40 }
41 }
42
43 private string nodeName;
44 /// <summary>
45 /// 节点名称(最多六个字符)
46 /// </summary>
47 public string NodeName
48 {
49 get
50 {
51 return nodeName;
52 }
53 set
54 {
55 nodeName = value;
56 if (nodeName.Length > 6)
57 {
58 //⾮添加项考虑字符长度
59 if (this.isNodeAdd != true && this.isChildNodeAdd != true)
60 {
61 nodeName = nodeName.Substring(0, 6);
62 }
63 }
64 OnPropertyChanged("NodeName");
65 }
66 }
67
68 private bool isChildNode;
69 /// <summary>
70 /// 是否是⼦节点
71 /// </summary>
72 public bool IsChildNode
73 {
74 get
75 {
76 return isChildNode;
77 }
78 set
80 isChildNode = value;
81 }
82 }
83
84 private bool isNodeAdd;
85 /// <summary>
86 /// 是否添加节点
87 /// </summary>
88 public bool IsNodeAdd
89 {
90 get
91 {
92 return isNodeAdd;
93 }
94 set
95 {
96 isNodeAdd = value;
97 }
98 }
99
100 private bool isChildNodeAdd;
101 /// <summary>
102 /// 是否是添加⼦节点
103 /// </summary>
104 public bool IsChildNodeAdd
105 {
106 get
107 {
108 return isChildNodeAdd;
109 }
110 set
111 {
112 isChildNodeAdd = value;
113 }
114 }
115
116 private ObservableCollection<TreeViewNode> childNodes;
117 /// <summary>
118 /// ⼦节点数据
119 /// </summary>
120 public ObservableCollection<TreeViewNode> ChildNodes
121 {
122 get
123 {
124 if (childNodes == null)
125 {
126 childNodes = new ObservableCollection<TreeViewNode>();
127 childNodes.CollectionChanged += new NotifyCollectionChangedEventHandler(OnMoreStuffChanged); 128 }
129 return childNodes;
130 }
131 set
132 {
133 childNodes = value;
134 }
135 }
136 private void OnMoreStuffChanged(object sender, NotifyCollectionChangedEventArgs e)
137 {
138 if (e.Action == NotifyCollectionChangedAction.Add)
139 {
140 TreeViewNode stuff = (TreeViewNode)e.NewItems[0];
141 stuff.ParentId = this.Id;
142 }
143 else if (e.Action == NotifyCollectionChangedAction.Remove)
145 TreeViewNode stuff = (TreeViewNode)e.OldItems[0];
146 if (stuff.ParentId == this.Id)
147 {
148 stuff.ParentId = 0;
149 }
150 }
151 }
152
153 #region 界⾯展⽰相关属性
154 //根据节点类型设置Margin
155 public string Margining
156 {
157 get
158 {
159 double padLeft;
160 if (this.isChildNode == true || this.isNodeAdd == true)
161 {
162 padLeft = 36;
163 }
164 else
165 {
166 padLeft = 10;
167 }
168 return string.Format("{0},0,0,0", padLeft);
169 }
170 }
171
172 //添加节点按钮是否展⽰
173 public Visibility ShowAddButton
174 {
175 get
176 {
177 if (this.isChildNode == false && this.isNodeAdd == true && this.isChildNodeAdd == false) 178 return Visibility.Visible;
179 else
180 return Visibility.Collapsed;
181 }
182 }
183
184 //根据节点设置分隔线
185 public string ShowBorderThickness
186 {
187 get
188 {
189 if (this.isChildNode == false && this.isChildNodeAdd == false)
190 return string.Format("0,1,0,0");
191 else
192 return string.Format("0,0,0,0");
193 }
194 }
195
196 //根据⼦⽗节点设置字体⼤⼩
197 public int SetFontSize
198 {
199 get
200 {
201 if (this.isChildNode == true)
202 return 12;
203 else
204 return 14;
205 }
206 }
207
208 //根据⼦⽗节点设置字体宽度
209 public string SetFontWeight
210 {
211 get
212 {
213 if (this.isChildNode == true)
214 return "Normal";
215 else
216 return "Bold";
217 }
218 }
219
220 //根据⼦⽗节点设置字体颜⾊
221 public string SetForeground
222 {
223 get
224 {
225 if (this.isChildNode == true || this.isNodeAdd == true)
226 return "#999999";
227 else
228 return "#000000";
229 }
230 }
231
232 //根据⼦⽗节点设置背景颜⾊
233 public string SetBackground
234 {
235 get
236 {
237 if (this.isChildNode == true || this.isNodeAdd == true)
238 return "#ffffff";
239 else
240 return "#87CEEB";
241 }
242 }
243
244 //节点是否展开
245 public bool SetIsExpanded
246 {
247 get
248 {
249 if (this.isChildNode != true || this.isNodeAdd != true)
250 return false;
251 else
252 return true;
253 }
254 }
255 #endregion
256 #endregion
257
258 #region 构造函数
259 public TreeViewNode()
260 {
261 }
262
263 public TreeViewNode(int _id, int _parentId, bool _isChildNode, bool _isChildNodeAdd, bool _isNodeAdd, string _nodeName) 264 {
265 this.id = _id;
266 this.parentId = _parentId;
267 this.isChildNode = _isChildNode;
268 this.isChildNodeAdd = _isChildNodeAdd;
269 this.isNodeAdd = _isNodeAdd;
270 deName = _nodeName;
271 }
272 #endregion
273 }
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论