WPF 如何捕获 ContextMenuClosing 事件

发布于 2024-10-20 08:36:22 字数 2132 浏览 1 评论 0原文

我有以下 XAML:

<Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="20"/>
        </Grid.ColumnDefinitions>            
        <local:DropDownButton 
            HorizontalAlignment="Right"
            Grid.Column="2"
            Width="18"
            Style="{StaticResource OrangeButton}"   
            ContextMenuClosing="colorPallete_ContextMenuClosing"
            x:Name="btnSelectColor">            
            <Polygon Points="0,0,5,4,10,0" Fill="Black"/>                                              
            <local:DropDownButton.DropDown>
                <ContextMenu StaysOpen="True" Name="colorPallete" ContextMenuClosing="colorPallete_ContextMenuClosing">
                    <MenuItem StaysOpenOnClick="True" OverridesDefaultStyle="True" ContextMenuClosing="colorPallete_ContextMenuClosing">
                        <MenuItem.Header>
                            <local:ColorPickerPopup x:Name="colorPicker" ContextMenuClosing="colorPallete_ContextMenuClosing"/>
                        </MenuItem.Header>                                                
                    </MenuItem>                                            
                </ContextMenu>                                        
            </local:DropDownButton.DropDown>
        </local:DropDownButton>            

        <Rectangle Width="17.5" Stroke="Black" Margin="0" 
                   Fill="{DynamicResource CheckerBrush}"/>            

        <Rectangle Width="17.5" Margin="0" Name="rtcColorPreview" />
        <TextBox Margin="2,0,0,0" Grid.Column="1" 
                 Width="100" BorderThickness="0"                     
                 Text="{Binding ElementName=colorPicker, Mode=TwoWay,  Path=SelectedColorName}"/>

    </Grid>

当标记为 colrPallete 的 ContextMenu 关闭时,不会调用事件处理程序 colorPallete_ContextMenuClosing。我似乎无法弄清楚缺少什么。

请帮忙! TIA。

I have the following XAML:

<Grid >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="20"/>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="20"/>
        </Grid.ColumnDefinitions>            
        <local:DropDownButton 
            HorizontalAlignment="Right"
            Grid.Column="2"
            Width="18"
            Style="{StaticResource OrangeButton}"   
            ContextMenuClosing="colorPallete_ContextMenuClosing"
            x:Name="btnSelectColor">            
            <Polygon Points="0,0,5,4,10,0" Fill="Black"/>                                              
            <local:DropDownButton.DropDown>
                <ContextMenu StaysOpen="True" Name="colorPallete" ContextMenuClosing="colorPallete_ContextMenuClosing">
                    <MenuItem StaysOpenOnClick="True" OverridesDefaultStyle="True" ContextMenuClosing="colorPallete_ContextMenuClosing">
                        <MenuItem.Header>
                            <local:ColorPickerPopup x:Name="colorPicker" ContextMenuClosing="colorPallete_ContextMenuClosing"/>
                        </MenuItem.Header>                                                
                    </MenuItem>                                            
                </ContextMenu>                                        
            </local:DropDownButton.DropDown>
        </local:DropDownButton>            

        <Rectangle Width="17.5" Stroke="Black" Margin="0" 
                   Fill="{DynamicResource CheckerBrush}"/>            

        <Rectangle Width="17.5" Margin="0" Name="rtcColorPreview" />
        <TextBox Margin="2,0,0,0" Grid.Column="1" 
                 Width="100" BorderThickness="0"                     
                 Text="{Binding ElementName=colorPicker, Mode=TwoWay,  Path=SelectedColorName}"/>

    </Grid>

The event handler colorPallete_ContextMenuClosing is not being called when ContextMenu labelled colrPallete is closing. I cannot seem to figure out what is missing.

Please help! TIA.

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

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

发布评论

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

评论(1

哆兒滾 2024-10-27 08:36:22

根据 MSDN 文档...

ContextMenu 本身是一个
FrameworkElement 派生类,但是
ContextMenuClosing 事件不会
直接由上下文菜单引发。
相反,该事件是从
“拥有”上下文菜单的元素
作为财产,仅在以下情况下提出:
用户尝试关闭上下文
用户界面中的菜单。

您需要调整代码,以便像您所做的那样仅在 DropDownButton 上定义处理程序。如果存在嵌套的 ContextMenu ,则嵌套的 ContextMenu 显然会引发该事件。

<local:DropDownButton ContextMenuClosing="colorPallete_ContextMenuClosing">
        ...                             
</local:DropDownButton>

使用 Button 看起来像这样......

<Button ContextMenuClosing="ContextMenu_ContextMenuClosing">
    <Button.ContextMenu>
        <ContextMenu>
             <MenuItem Header="Go"/>
        </ContextMenu>
    </Button.ContextMenu>
 </Button>

当包含 MenuItemContextMenu 关闭时;将引发该事件并调用处理程序。

不确定您使用的 DropDownButton 控件是什么,因此我无法评论 DropDown 属性是什么以及您如何嵌套 ContextMenu

Per the MSDN documentation...

ContextMenu itself is a
FrameworkElement derived class, but
the ContextMenuClosing event will not
be raised by a context menu directly.
Instead, the event is raised from the
element that "owns" the context menu
as a property and is only raised when
a user attempts to close a context
menu in the UI.

You would need to adjust your code so that the handler is defined solely on the DropDownButton as you have done. If there is a nested ContextMenu then the nested ContextMenu will obviously raise the event.

<local:DropDownButton ContextMenuClosing="colorPallete_ContextMenuClosing">
        ...                             
</local:DropDownButton>

Using a Button it would look like this...

<Button ContextMenuClosing="ContextMenu_ContextMenuClosing">
    <Button.ContextMenu>
        <ContextMenu>
             <MenuItem Header="Go"/>
        </ContextMenu>
    </Button.ContextMenu>
 </Button>

..where when the ContextMenu containing the MenuItem closes; the event will be raised and the handler will be called.

Not certain what DropDownButton control you are using so I can't comment on what the DropDown property is and how you are nesting your ContextMenu.

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