每当菜单项的子菜单打开时,我想处理一个事件。关闭也一样。我怎样才能做到这一点?
问问题
8053 次
3 回答
15
很容易。订阅 aMenuItem
的SubmenuOpened
事件。传统的做法:
MidItem.SubmenuOpened += new RoutedEventHandler(MidItem_SubmenuOpened);
private void MidItem_SubmenuOpened(object sender, RoutedEventArgs e)
{
//Menu Open Logic Here
}
或者酷炫的动态方法:
MidItem.SubmenuOpened += delegate(object sender, RoutedEventArgs e)
{
//Menu Open Logic Here
};
于 2010-02-19T14:55:43.973 回答
1
不确定您想要什么,但请查看这些事件ContextMenu
和MenuItem
:
ContextMenu
:
http://msdn.microsoft.com/en-us/library/system.windows.controls.contextmenu_events.aspx
MenuItem
:
http://msdn.microsoft.com/en-us/library/system.windows.controls.menuitem_events.aspx
我认为ContextMenuOpening
并且ContextMenuClosing
可能很有趣;)
于 2010-02-18T15:50:19.877 回答
0
如果您希望在真正开放之前打开,您可以将 menuitem 子类化
Imports System.ComponentModel
Public Class CustomMenuItem
Inherits MenuItem
Public Event SubMenuOpening(sender As Object, e As CancelEventArgs)
Public Event SubMenuClosing(sender As Object, e As CancelEventArgs)
Protected Overrides Sub OnPropertyChanged(e As DependencyPropertyChangedEventArgs)
If e.Property Is MenuItem.IsSubmenuOpenProperty Then
Dim MyCancelEventArgs As New CancelEventArgs
If e.NewValue Then
RaiseEvent SubMenuOpening(Me, MyCancelEventArgs)
If MyCancelEventArgs.Cancel Then Me.IsSubmenuOpen = False
Else
RaiseEvent SubMenuClosing(Me, MyCancelEventArgs)
If MyCancelEventArgs.Cancel Then Me.IsSubmenuOpen = True
End If
End If
MyBase.OnPropertyChanged(e)
End Sub
End Class
我知道这是 VB,但它很容易转换。另外,使用 CancelEventArgs,您可以模拟取消打开(或关闭)。
于 2020-05-01T12:34:05.397 回答