由于Writer嫌我文章过长,只能把上篇拆开两半了。以下是接着上篇的。
准备工作做完了,现在就要完成点击事件。
定义Expander和单击事件:
1: /// <summary>
2: /// The togglebutton for expanding or collapsing the items when mouse left button clicked on it
3: /// </summary>
4: private ToggleButton expander;
5:
1: /// <summary>
2: /// Gets or sets the toggle button Expander
3: /// </summary>
4: public ToggleButton Expander
5: {
6: get
7: {
8: return this.expander;
9: }
10:
11: set
12: {
13: if (this.expander != null)
14: {
15: this.expander.Click -= new RoutedEventHandler(this.OnExpanderClick);
16: }
17:
18: this.expander = value;
19:
20: if (this.expander != null)
21: {
22: this.expander.IsChecked = new bool?(this.IsExpanded);
23: this.expander.Click += new RoutedEventHandler(this.OnExpanderClick);
24: }
25: }
26: }
27:
1: /// <summary>
2: /// Method occurs when mouse left button click on Expander
3: /// </summary>
4: /// <param name="sender">The source object</param>
5: /// <param name="e">The routed event arrguments</param>
6: private void OnExpanderClick(object sender, RoutedEventArgs e)
7: {
8: bool flag = !this.IsExpanded;
9: this.IsExpanded = flag;
10: }
11:
1: /// <summary>
2: /// Method occurs when the expanded state has been changed
3: /// </summary>
4: /// <param name="handler">The target event handler</param>
5: /// <param name="args">The routed arrguments</param>
6: private void ChangeExpandedState(RoutedEventHandler handler, RoutedEventArgs args)
7: {
8: ToggleButton button = this.Expander;
9:
10: if (button != null)
11: {
12: button.IsChecked = new bool?(this.IsExpanded);
13: }
14:
15: this.UpdateVisualState(true);
16: this.RaiseEvent(handler, args);
17: }
18:
接下来就是最后一步了,就是定义展开和收起事件,并把它们和刚才的准备工作结合起来
定义event handler:
1: #region Events
2:
3: /// <summary>
4: /// The expanded event
5: /// </summary>
6: public event RoutedEventHandler Expanded;
7:
8: /// <summary>
9: /// The Collapsed event
10: /// </summary>
11: public event RoutedEventHandler Collapsed;
12:
13: #endregion
14:
定义事件:
1: /// <summary>
2: /// To expand the children
3: /// </summary>
4: /// <param name="e">The routed event arrguments</param>
5: public void OnExpanded(RoutedEventArgs e)
6: {
7: this.ChangeExpandedState(this.Expanded, e);
8: }
9:
10: /// <summary>
11: /// To collapse the children
12: /// </summary>
13: /// <param name="e">The routed event arrguments</param>
14: public void OnCollapsed(RoutedEventArgs e)
15: {
16: this.ChangeExpandedState(this.Collapsed, e);
17: }
18:
修改OnIsExpandedPropertyChanged方法:
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: FancyTreeViewItem item = o as FancyTreeViewItem;
9: bool newValue = (bool)e.NewValue;
10: RoutedEventArgs args = new RoutedEventArgs();
11:
12: if (newValue)
13: {
14: item.OnExpanded(args);
15: }
16: else
17: {
18: item.OnCollapsed(args);
19: }
20: }
21:
从模板中寻找Expander模块跟类中Expander按钮对应起来:
1: /// <summary>
2: /// Override the function for banding template
3: /// </summary>
4: public override void OnApplyTemplate()
5: {
6: this.Expander = this.GetTemplateChild("Expander") as ToggleButton;
7: base.OnApplyTemplate();
8: }
9:
最后运行看下效果:
图3.1 节点收起的效果图
图3.2 节点展开的效果图