DevExpress的NavBarControl组件中NavBarGroup如何触发展开或者折叠事件
NavBarControl--NavBarGroup--NavBarItem
NavBarControl控件虽然很好用,但是有一点很讨厌,就是NavBarGroup没有公开出点击事件或者折叠事件,对我们的开发造成很多的不便...但是也不是没有办法解决!
我们都知道NavBarGroup的折叠是由Expanded属性控制的,那么这就好办啦.
转到NavBarGroup的定义Expanded属性:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using System.Linq; using System.Text; using DevExpress.XtraNavBar; namespace WMS.Ctrl { public partial class MyNavBarGroup : NavBarGroup { public MyNavBarGroup() : base() { } public MyNavBarGroup(string caption) : base(caption) { } //重写展开折叠属性,在set中触发自己定义的事件 private bool _Expanded = false; /// <summary> /// 展开折叠属性 /// </summary> public override bool Expanded { get { return _Expanded; } set { //触发改变之前的事件 CancelEventArgs args = new CancelEventArgs() { Cancel = false }; OnExpandChanging(args); //是否可以展开,是否取消,是否与上次一值想等 if (CanExpand && !args.Cancel && _Expanded != value) { base.Expanded = _Expanded = value; //触发修改之后的事件 OnExpandChanged(new ExpandedEventArgs(value)); } } } //自己加了一个属性,是否能够折叠或者展开,有的时候我们需要这个功能 private bool _CanExpand = true; /// <summary> /// 是否能够展开折叠(为false时,则不能展开或者折叠) /// </summary> public virtual bool CanExpand { get { return _CanExpand; } set { _CanExpand = value; } } /// <summary> /// 展开或者折叠之后 /// </summary> public event EventHandler<ExpandedEventArgs> ExpandChanged; protected virtual void OnExpandChanged(ExpandedEventArgs e) { if (ExpandChanged != null) { ExpandChanged(this, e); } } /// <summary> /// 展开或者折叠之前 /// </summary> public event EventHandler<CancelEventArgs> ExpandChanging; protected virtual void OnExpandChanging(CancelEventArgs e) { if (ExpandChanging != null) { ExpandChanging(this, e); } } } public class ExpandedEventArgs : EventArgs { public ExpandedEventArgs() { Expanded = false; } public ExpandedEventArgs(bool expanded) { Expanded = expanded; } /// <summary> /// 展开或者折叠 /// </summary> public bool Expanded { get; set; } } }
//调用
MyNavBarGroup m = new MyNavBarGroup("Test MyGroup"); m.ExpandChanged += new EventHandler<ExpandedEventArgs>(m_ExpandChanged); m.ExpandChanging += new EventHandler<CancelEventArgs>(m_ExpandChanging); navBarControl1.Groups.Add(m);