ContextMenu一些记录

本文介绍如何使用Flex中的ContextMenu类来控制上下文菜单的显示,包括添加自定义菜单项、控制内置上下文菜单项以及为顶层组件创建上下文菜单。此外,还特别提到TextField对象的上下文菜单中会始终包含剪贴板菜单。

摘自API,自己复制一下,方便查看关键

通过 ContextMenu 类,可以控制上下文菜单中显示的项。

使用其方法和属性可以添加自定义菜单项、控制内置上下文菜单项(如“放大”和“打印”)的显示或者创建菜单的副本。

在 Flex 中,只有应用程序中的顶层组件才能拥有上下文菜单。例如,如果 DataGrid 控件是 TabNavigator 或 VBox 容器的子级,则 DataGrid 控件不能拥有其自己的上下文菜单。

为了向 ContextMenu 对象中添加新项,可以创建一个 ContextMenuItem 对象,然后将该对象添加到 ContextMenu.customItems 数组。

注意:TextField 对象的上下文菜单中始终包含剪贴板菜单。剪贴板菜单包含“剪切”、“复制”、“粘贴”、“清除”和“全选”命令。您不能从 TextField 对象的上下文菜单中删除这些命令。对于 TextField 对象,选择这些命令(或等效键盘命令)不会生成 clearcopycutpasteselectAll 事件。

 

 

转载于:https://www.cnblogs.com/Null2051/archive/2012/10/03/2710903.html

### WPF 中 ContextMenu 弹出后原始控件焦点丢失解决方案 在 WPF 应用程序中,当 `ContextMenu` 被激活时,默认行为会使当前具有焦点的控件失去焦点。为了保持原有控件的焦点状态,在显示上下文菜单时不使该控件失焦,可以采取以下几种方法: #### 方法一:自定义附加属性控制 FocusManager 通过创建一个附加依赖项属性并将其应用于触发器或样式内,可以在打开 `ContextMenu` 后立即重新设置焦点到目标元素。 ```csharp public static class FocusHelper { public static readonly DependencyProperty KeepFocusProperty = DependencyProperty.RegisterAttached( "KeepFocus", typeof(bool), typeof(FocusHelper), new PropertyMetadata(false, OnKeepFocusChanged)); private static void OnKeepFocusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var uiElement = (UIElement)d; if ((bool)e.NewValue && uiElement.ContextMenu != null) { uiElement.PreviewMouseRightButtonDown += UiElement_PreviewMouseRightButtonDown; } else { uiElement.PreviewMouseRightButtonDown -= UiElement_PreviewMouseRightButtonDown; } } private static void UiElement_PreviewMouseRightButtonDown(object sender, MouseButtonEventArgs e) { UIElement element = sender as UIElement; if (element?.ContextMenu is { IsOpen: false } contextMenu) { contextMenu.Opened += delegate { Keyboard.Focus(element); }; } } public static bool GetKeepFocus(UIElement target) { return (bool)target.GetValue(KeepFocusProperty); } public static void SetKeepFocus(UIElement target, bool value) { target.SetValue(KeepFocusProperty, value); } } ``` 使用上述代码片段中的可以通过 XAML 设置特定控件上的这个新属性[^1]: ```xml <Button Content="Click Me" local:FocusHelper.KeepFocus="True"> </Button> ``` #### 方法二:重写 ContextMenu 的 Opened 事件处理逻辑 另一种方式是在每次 `ContextMenu` 打开之前保存当前聚焦的对象,并在其关闭之后恢复它。这通常涉及到订阅 `Opened` 和 `Closed` 两个事件。 ```csharp private FrameworkElement _focusedElement; // 在初始化或其他适当的地方注册这些事件处理器... contextMenu.Opened += ContextMenu_Opened; contextMenu.Closed += ContextMenu_Closed; void ContextMenu_Opened(object sender, RoutedEventArgs e) { // 记录下当前获得键盘输入焦点的控件 _focusedElement = Keyboard.FocusedElement as FrameworkElement; } void ContextMenu_Closed(object sender, RoutedEventArgs e) { // 如果记录下来的控件仍然有效,则尝试再次给予其焦点 if (_focusedElement != null && !_focusedElement.IsFocused) { _focusedElement.Focus(); } } ``` 这种方法适用于更广泛的情况,尤其是当你想要在整个应用程序范围内实现这一功能而不仅仅是针对单个控件时[^2]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值