silverlight实现多级右键菜单
2011-11-25 15:34
447 查看
Silverlight 4新增了右键菜单功能,通过contextMenu控件来实现右击菜单的功能.简单的右键菜单功能非常容易实现,网上很多朋友都有提供示例分享,最近因为项目需要得实现多级右键菜单功能,效果如下:
SuperMenuItem
所有源代码都已经附上,有需要的自己copy到项目中,不单独提供源代码下载。
SuperMenuItem
using System; using System.Collections.Specialized; using System.Windows; using System.Windows.Controls; using System.Windows.Controls.Primitives; using System.Windows.Input; namespace Demo { public class SuperMenuItem : MenuItem { #region Fields private Popup popup; public bool CanLeave { get; set; } #endregion #region Properties public Visibility HasSubItems { get { return (Visibility)GetValue(HasSubItemsProperty); } set { SetValue(HasSubItemsProperty, value); } } public static readonly DependencyProperty HasSubItemsProperty = DependencyProperty.Register("HasSubItems", typeof(Visibility), typeof(SuperMenuItem), new PropertyMetadata(Visibility.Collapsed)); public bool IsSubmenuOpen { get { return (bool)GetValue(IsSubmenuOpenProperty); } set { SetValue(IsSubmenuOpenProperty, value); } } public static readonly DependencyProperty IsSubmenuOpenProperty = DependencyProperty.Register("IsSubmenuOpen", typeof(bool), typeof(SuperMenuItem), new PropertyMetadata(false)); #endregion #region Constructor public SuperMenuItem() { this.DefaultStyleKey = typeof(SuperMenuItem); this.MouseEnter += new MouseEventHandler(parent_MouseEnter); this.MouseLeave += new MouseEventHandler(SuperMenuItem_MouseLeave); this.Click += new RoutedEventHandler(SuperMenuItem_Click); this.CanLeave = true; } private void SuperMenuItem_Click(object sender, RoutedEventArgs e) { if (this.Parent != null && this.Parent is SuperMenuItem) { (this.Parent as SuperMenuItem).OnClick(); } } public override void OnApplyTemplate() { base.OnApplyTemplate(); popup = (Popup)this.GetTemplateChild("PART_Popup"); popup.Opened += new EventHandler(popup_Opened); popup.Closed += new EventHandler(popup_Closed); } private void popup_Opened(object sender, EventArgs e) { this.CanLeave = false; } private void popup_Closed(object sender, EventArgs e) { if (this.HasSubItems == Visibility.Visible) { this.IsSubmenuOpen = false; } } protected override void OnItemsChanged(NotifyCollectionChangedEventArgs e) { if (e.NewItems != null) { if (e.NewItems.Count > 0) { this.HasSubItems = Visibility.Visible; } } } private void parent_MouseEnter(object sender, MouseEventArgs e) { this.CanLeave = true; if (this.HasSubItems == Visibility.Visible) { this.IsSubmenuOpen = true; } if (this.Parent != null && this.Parent is ContextMenu) { foreach (var item in (this.Parent as ContextMenu).Items) { if (item != this) { (item as SuperMenuItem).IsSubmenuOpen = false; } } } } private void SuperMenuItem_MouseLeave(object sender, MouseEventArgs e) { if (this.HasSubItems == Visibility.Visible) { if (CanLeave) { this.IsSubmenuOpen = false; } } } #endregion } }
所有源代码都已经附上,有需要的自己copy到项目中,不单独提供源代码下载。
相关文章推荐
- Silverlight 4的是鼠标右键菜单的实现(一)
- C#实现Silverlight中右键菜单
- Silverlight 4的是鼠标右键菜单的实现(二)
- COM组件实现右键自定义多级菜单
- Silverlight系列(右键菜单、控件移动的实现)
- js实现完全自定义可带多级目录的网页鼠标右键菜单方法
- Silverlight 4的是鼠标右键菜单的实现(三)
- js实现完全自定义可带多级目录的网页鼠标右键菜单方法
- silverlight右键菜单实现
- Silverlight系列(右键菜单、控件移动的实现)
- 注册表实现右键自定义多级菜单
- ArcGIS API for Silverlight开发中鼠标左键点击地图上的点弹出窗口及右键点击弹出快捷菜单的实现代码
- js实现右键弹出多级菜单特效
- 用popup实现右键多级菜单
- 在Silverlight 2 Beta2 中实现菜单(Menu)——数据可源自SharePoint
- Java实现右键弹出菜单例子
- JavaScript实现横向滑出的多级菜单效果
- JS实多级联动下拉菜单类,简单实现省市区联动菜单!
- VC中实现在指定控件上弹出右键菜单