您好 我试图摆脱烦人的“关于Silverlight”上下文菜单,只要您在Silverlight应用程序中单击鼠标右键就会弹出。我添加了通常的方法:
在App.xaml中 rootVisual.MouseRightButtonDown + =((s,args)=> args.Handled = true);
和所有ChildWindows相同。 持续存在的问题在于所有“弹出”控件,如组合框和日期选择器日历弹出窗口。在那里,我无法摆脱它。我想在一个我可以为整个应用程序隐含的样式中处理右键单击。这可能吗?我能以其他聪明的方式解决它吗?
最佳
丹尼尔
答案 0 :(得分:6)
答案是继承组合框并制作这样的自定义控件:
public class CellaComboBox : ComboBox
{
public CellaComboBox()
{
DropDownOpened += _dropDownOpened;
DropDownClosed += _dropDownClosed;
}
private static void _dropDownClosed(object sender, EventArgs e)
{
HandlePopupRightClick(sender, false);
}
private static void _dropDownOpened(object sender, EventArgs e)
{
HandlePopupRightClick(sender, true);
}
private static void HandlePopupRightClick(object sender, bool hook)
{
ComboBox box = (ComboBox)sender;
var popup = box.GetChildElement<Popup>();
if (popup != null)
{
HookPopupEvent(hook, popup);
}
}
static void HookPopupEvent(bool hook, Popup popup)
{
if (hook)
{
popup.MouseRightButtonDown += popup_MouseRightButtonDown;
popup.Child.MouseRightButtonDown += popup_MouseRightButtonDown;
}
else
{
popup.MouseRightButtonDown -= popup_MouseRightButtonDown;
popup.Child.MouseRightButtonDown -= popup_MouseRightButtonDown;
}
}
static void popup_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
e.Handled = true;
}
使用framworkelement的扩展方法,如下所示:
public static class FrameworkElementExtensions
{
public static TType GetChildElement<TType>(this DependencyObject parent) where TType : DependencyObject
{
TType result = default(TType);
if (parent != null)
{
result = parent as TType;
if (result == null)
{
for (int childIndex = 0; childIndex < VisualTreeHelper.GetChildrenCount(parent); ++childIndex)
{
var child = VisualTreeHelper.GetChild(parent, childIndex) as FrameworkElement;
result = GetChildElement<TType>(child) as TType;
if (result != null) return result;
}
}
}
return result;
}
}
您需要以相同的方式处理DatePicker,而不是使用DropDownOpened和DropDownClosed,而是使用CalenderOpened和CalenderClosed
答案 1 :(得分:2)
C#Corner有一篇文章用于修复Silverlight 3上的弹出窗口: