扩展WPF工具包ColorPicker:如何使用ContextMenu事件? [英] Extended WPF Toolkit ColorPicker: How to use ContextMenu events?
本文介绍了扩展WPF工具包ColorPicker:如何使用ContextMenu事件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
情况:我在Canvas中有ColorPicker。
目标:使用ContextMenu的事件。
问题:ContextMenuOpening事件不会触发和ContextMenu.Opened事件结果错误很多其他事件。
注意:
1.我的意思是ContextMenu的下拉菜单,但我不知道ContextMenu究竟是什么。
2.它是扩展WPF工具包并使用它你需要添加扩展dll。
代码:
Situation: I have ColorPicker in Canvas.
Goal: To use events of ContextMenu.
Problem: The ContextMenuOpening event doesn't fire and the ContextMenu.Opened event results error as many other events.
Notes:
1. I mean a drop menu by "ContextMenu", but i don't know what "ContextMenu" exactly is.
2. It is Extended WPF Toolkit and to use it you need to add extended dlls.
Code:
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
canvas.Background = Brushes.Black;
this.Content = canvas;
ColorPicker colorpicker = new ColorPicker();
canvas.Children.Add(colorpicker);
colorpicker.ContextMenuOpening += delegate { System.Windows.MessageBox.Show("a"); }; // Doesn't fire.
colorpicker.ContextMenu.Opened += delegate { System.Windows.MessageBox.Show("a"); }; // Error: Object reference not set to an instance of an object.
}
Canvas canvas = new Canvas();
}
推荐答案
上下文菜单是右键单击某些内容时出现的菜单。颜色选择器的弹出窗口不是上下文菜单,这就是ContextMenuOpening
事件不会触发的原因。除非你明确设置ContextMenu
属性,否则它将返回null
,这就是为什么你得到一个NullReferenceException
当您尝试订阅它的已打开的
事件时。
查看源代码 [ ColorPicker 控件,title =新窗口> ^ ],当弹出窗口打开时,它似乎不会触发事件。您最好的选择可能是订阅IsOpen
属性的属性更改事件:
A context menu is the menu that appears when you right-click on something. The color-picker's popup isn't a context menu, which is why theContextMenuOpening
event doesn't fire. And unless you've explicitly set theContextMenu
property, it will returnnull
, which is why you're getting aNullReferenceException
when you try to subscribe to it'sOpened
event.
Looking at the source code[^] for theColorPicker
control, it doesn't seem to fire an event when the popup opens. Your best bet is probably to subscribe to the property change event for theIsOpen
property:
DependencyPropertyDescriptor descriptor = DependencyPropertyDescriptor.FromProperty(ColorPicker.IsOpenProperty, typeof(ColorPicker));
descriptor.AddValueChanged(colorpicker, delegate
{
if (colorpicker.IsOpen)
{
// Do something here...
}
});
这篇关于扩展WPF工具包ColorPicker:如何使用ContextMenu事件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文