Silverlight自定义控件系列 – TreeView (3) 添加展开和收起事件

Silverlight自定义控件系列 – TreeView (3) 添加展开和收起事件,第1张

概述  由于Writer嫌我文章过长,只能把上篇拆开两半了。以下是接着上篇的。 准备工作做完了,现在就要完成点击事件。 定义Expander和单击事件: 1: /// <summary> 2: /// The togglebutton for expanding or collapsing the items when mouse left button clicked on it 3: ///  

由于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 节点展开的效果图

总结

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

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

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

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

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

发表评论

登录后才能评论

评论列表(0条)

保存