菜单项打开和关闭事件

发布于 2024-08-22 04:30:17 字数 42 浏览 7 评论 0原文

每当菜单项的子菜单打开时,我想处理一个事件。关闭时也一样。我该怎么做?

I want to handle an event, whenever the Submenus of a menu item opens. Same for closing. how can I do this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

青萝楚歌 2024-08-29 04:30:17

很容易。订阅 MenuItemSubmenuOpened 事件。传统的方法:

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
};

Very easy. Subscribe to a MenuItem's SubmenuOpened event. Traditional way to do it:

MidItem.SubmenuOpened += new RoutedEventHandler(MidItem_SubmenuOpened);

private void MidItem_SubmenuOpened(object sender, RoutedEventArgs e)
{
    //Menu Open Logic Here
}

Or the cool dynamic method:

MidItem.SubmenuOpened += delegate(object sender, RoutedEventArgs e)
{
    //Menu Open Logic Here
};
遗失的美好 2024-08-29 04:30:17

不确定您想要什么,但请查看 ContextMenuMenuItem 的这些事件:

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

我认为 ContextMenuOpeningContextMenuClosing< /code> 可能很有趣;)

Not sure what you want, but take a look at these events for ContextMenu and 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

I think ContextMenuOpening and ContextMenuClosing might be interesting ;)

却一份温柔 2024-08-29 04:30:17

如果您希望在真正的打开之前打开,您可以对菜单项进行子类化,

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,您可以模拟取消打开(或关闭)。

If you wish a opening BEFORE the real opening, you can subclass the 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

I know this is VB, but it's easily convertible.
Plus, with the CancelEventArgs, you can simulate cancel the opening (or closing).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文