Dan*_*oft 5 silverlight silverlight-4.0
嗨,我正试图摆脱烦人的"关于Silverlight"上下文菜单,只要您在Silverlight应用程序中单击鼠标右键菜单就会弹出.我添加了通常的方法:
在App.xaml中
rootVisual.MouseRightButtonDown + =((s,args)=> args.Handled = true);
所有ChildWindows都是一样的.持续存在的问题在于所有"弹出"控件,如组合框和日期选择器日历弹出窗口.在那里,我无法摆脱它.我想在一个我可以为整个应用程序隐含的样式中处理右键单击.这可能吗?我能解决一些其他智能方式吗?
最好的
丹尼尔
答案是继承组合框并制作如下的自定义控件:
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;
}
Run Code Online (Sandbox Code Playgroud)
使用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;
}
}
Run Code Online (Sandbox Code Playgroud)
您需要以相同的方式处理DatePicker,而不是DropDownOpened和DropDownClosed,您使用CalenderOpened和CalenderClosed
| 归档时间: |
|
| 查看次数: |
3105 次 |
| 最近记录: |