gpt4 book ai didi

WPF如何捕获ContextMenuClosing事件

转载 作者:行者123 更新时间:2023-12-01 19:36:26 26 4
gpt4 key购买 nike

我有以下 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。

最佳答案

根据 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.

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

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

使用按钮它看起来像这样......

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

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

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

关于WPF如何捕获ContextMenuClosing事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5197226/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com