WPF 如何捕获 ContextMenuClosing 事件
我有以下 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
根据 MSDN 文档...
您需要调整代码,以便像您所做的那样仅在
DropDownButton
上定义处理程序。如果存在嵌套的 ContextMenu ,则嵌套的 ContextMenu 显然会引发该事件。使用
Button
看起来像这样......当包含
MenuItem
的ContextMenu
关闭时;将引发该事件并调用处理程序。不确定您使用的
DropDownButton
控件是什么,因此我无法评论DropDown
属性是什么以及您如何嵌套ContextMenu
。Per the MSDN documentation...
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 nestedContextMenu
then the nestedContextMenu
will obviously raise the event.Using a
Button
it would look like this.....where when the
ContextMenu
containing theMenuItem
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 theDropDown
property is and how you are nesting yourContextMenu
.