- WPF 教程
- WPF - 首页
- WPF - 概述
- WPF - 环境设置
- WPF - Hello World
- WPF - XAML 概述
- WPF - 元素树
- WPF - 依赖属性
- WPF - 路由事件
- WPF - 控件
- WPF - 布局
- WPF - 布局嵌套
- WPF - 输入
- WPF - 命令行
- WPF - 数据绑定
- WPF - 资源
- WPF - 模板
- WPF - 样式
- WPF - 触发器
- WPF - 调试
- WPF - 自定义控件
- WPF - 异常处理
- WPF - 本地化
- WPF - 交互
- WPF - 2D 图形
- WPF - 3D 图形
- WPF - 多媒体
- WPF 有用资源
- WPF - 快速指南
- WPF - 有用资源
- WPF - 讨论
WPF - 上下文菜单
ContextMenu 是一个弹出菜单,它使控件能够公开特定于控件上下文的函数。只要用户界面从该元素内部请求上下文菜单,它就会出现。ContextMenu 类的层次继承如下:
ContextMenu 的常用属性
| 序号 | 属性及说明 |
|---|---|
| 1 | Background 获取或设置提供控件背景的画刷。(从 Control 继承) |
| 2 | BorderThickness 获取或设置控件的边框粗细。(从 Control 继承) |
| 3 | ContextMenu 获取或设置应在用户界面 (UI) 从该元素内部请求上下文菜单时出现的上下文菜单元素。(从 FrameworkElement 继承。) |
| 4 | FontFamily 获取或设置用于在控件中显示文本的字体。(从 Control 继承) |
| 5 | FontSize 获取或设置此控件中文本的大小。(从 Control 继承) |
| 6 | FontStyle 获取或设置呈现文本的样式。(从 Control 继承) |
| 7 | FontWeight 获取或设置指定字体的粗细。(从 Control 继承) |
| 8 | Foreground 获取或设置描述前景色颜色的画刷。(从 Control 继承) |
| 9 | GroupStyle 获取定义每个组级别外观的 GroupStyle 对象集合。(从 ItemsControl 继承) |
| 10 | HasItems 获取一个值,该值指示 ItemsControl 是否包含项。(从 ItemsControl 继承。) |
| 11 | Height 获取或设置 FrameworkElement 的建议高度。(从 FrameworkElement 继承) |
| 12 | HorizontalAlignment 获取或设置在 FrameworkElement 组合在布局父级(例如面板或项控件)中时应用的水平对齐特征。(从 FrameworkElement 继承) |
| 13 | IsFocused 获取一个值,该值确定此元素是否具有逻辑焦点。这是一个依赖属性。(从 UIElement 继承。) |
| 14 | IsOpen 获取或设置一个值,该值指示 ContextMenu 是否可见。 |
| 15 | IsEnabled 获取或设置一个值,指示用户是否可以与控件交互。(从 Control 继承) |
| 16 | ItemsSource 获取或设置用于生成 ItemsControl 内容的对象源。(从 ItemsControl 继承) |
| 17 | Margin 获取或设置 FrameworkElement 的外边距。(从 FrameworkElement 继承) |
| 18 | Name 获取或设置对象的标识名称。当 XAML 处理器从 XAML 标记创建对象树时,运行时代码可以通过此名称引用 XAML 声明的对象。(从 FrameworkElement 继承) |
| 19 | Opacity 获取或设置对象的透明度。(从 UIElement 继承) |
| 20 | Style 获取或设置在此对象布局和呈现期间应用的实例样式。(从 FrameworkElement 继承) |
| 21 | VerticalAlignment 获取或设置在 FrameworkElement 组合在父对象(例如面板或项控件)中时应用的垂直对齐特征。(从 FrameworkElement 继承) |
| 22 | Width 获取或设置 FrameworkElement 的宽度。(从 FrameworkElement 继承) |
ContextMenu 的常用方法
| 序号 | 方法及说明 |
|---|---|
| 1 | AddChild 将指定的对象作为 ItemsControl 对象的子级添加。(从 ItemsControl 继承。) |
| 2 | Arrange 定位子对象并确定 UIElement 的大小。实现其子元素自定义布局的父对象应从其布局覆盖实现中调用此方法,以形成递归布局更新。(从 UIElement 继承) |
| 3 | FindName 检索具有指定标识符名称的对象。(从 FrameworkElement 继承) |
| 4 | Focus 尝试将焦点设置到控件上。(从 Control 继承) |
| 5 | GetValue 从 DependencyObject 返回依赖属性的当前有效值。(从 DependencyObject 继承) |
| 6 | IsItemItsOwnContainer 确定指定项是否(或是否有资格成为)其自己的容器。(从 ItemsControl 继承。) |
| 7 | OnDragEnter 在 DragEnter 事件发生之前调用。(从 Control 继承) |
| 8 | OnDragLeave 在 DragLeave 事件发生之前调用。(从 Control 继承) |
| 9 | OnDragOver 在 DragOver 事件发生之前调用。(从 Control 继承) |
| 10 | OnDrop 在 Drop 事件发生之前调用。(从 Control 继承) |
| 11 | OnContextMenuOpening 每当未处理的 ContextMenuClosing 路由事件在其路由中到达此类时都会调用。实现此方法以为此事件添加类处理。(从 FrameworkElement 继承。) |
| 12 | OnItemsChanged 当 Items 属性更改时调用。(从 ItemsControl 继承。) |
| 13 | OnLostFocus 在 LostFocus 事件发生之前调用。(从 Control 继承) |
| 14 | ReadLocalValue 如果设置了局部值,则返回依赖属性的局部值。(从 DependencyObject 继承) |
| 15 | SetBinding 使用提供的绑定对象将绑定附加到 FrameworkElement。(从 FrameworkElement 继承) |
| 16 | SetValue 设置 DependencyObject 上依赖属性的局部值。(从 DependencyObject 继承) |
ContextMenu 的常用事件
| 序号 | 事件及说明 |
|---|---|
| 1 | Closed 当 ContextMenu 的特定实例关闭时发生。 |
| 2 | ContextMenuClosing 在元素上的任何上下文菜单关闭之前发生。(从 FrameworkElement 继承。) |
| 3 | ContextMenuOpening 当元素上的任何上下文菜单打开时发生。(从 FrameworkElement 继承。) |
| 4 | DataContextChanged 当此元素的数据上下文更改时发生。(从 FrameworkElement 继承。) |
| 5 | DragEnter 当输入系统报告以该元素作为目标的基础拖动事件时发生。(从 UIElement 继承) |
| 6 | DragLeave 当输入系统报告以该元素作为源的基础拖动事件时发生。(从 UIElement 继承) |
| 7 | DragOver 当输入系统报告以该元素作为潜在放置目标的基础拖动事件时发生。(从 UIElement 继承) |
| 8 | Drop 当输入系统报告以该元素作为放置目标的基础放置事件时发生。(从 UIElement 继承) |
| 9 | GotFocus 当 UIElement 获取焦点时发生。(从 UIElement 继承) |
| 10 | IsEnabledChanged 当 IsEnabled 属性更改时发生。(从 Control 继承) |
| 11 | KeyDown 当 UIElement 具有焦点时按下键盘键时发生。(从 UIElement 继承) |
| 12 | KeyUp 当 UIElement 具有焦点时释放键盘键时发生。(从 UIElement 继承) |
| 13 | LostFocus 当 UIElement 失去焦点时发生。(从 UIElement 继承) |
示例
让我们创建一个名为 **WPFContextMenuControl** 的新 WPF 项目。
从工具箱中拖动一个文本框,并在属性窗口中设置以下属性。
| 属性 | 值 |
|---|---|
| Name | textBox1 |
| Text | 您好,这是 WPF 教程 |
| TextWraping | Wrap |
| Width | 300 |
现在切换到 XAML 窗口,您将在其中看到文本框和 ContextMenu 的 XAML 标记。
添加更多属性、菜单项以及选中和未选中事件,如下面的 XAML 代码所示。
以下示例包含一个带有 ContextMenu 的文本框,该菜单可操作文本框内的文本。以下 XAML 代码创建了一个带有某些属性和事件上下文菜单的文本框。
<Window x:Class = "WPFContextMenuControl.MainWindow"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d = "http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc = "http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local = "clr-namespace:WPFContextMenuControl"
mc:Ignorable = "d" Title = "MainWindow" Height = "350" Width = "604">
<Grid>
<TextBox x:Name = "textBox1" HorizontalAlignment = "Left" Height = "178"
Margin = "92,61,0,0" TextWrapping = "Wrap" Text = "Hi, this is WPF tutorial"
VerticalAlignment = "Top" Width = "306">
<TextBox.ContextMenu>
<ContextMenu>
<MenuItem Header = "_Bold" IsCheckable = "True"
Checked = "Bold_Checked" Unchecked = "Bold_Unchecked" />
<MenuItem Header = "_Italic" IsCheckable = "True"
Checked = "Italic_Checked" Unchecked = "Italic_Unchecked" />
<Separator />
<MenuItem Header = "Increase Font Size" Click = "IncreaseFont_Click" />
<MenuItem Header = "_Decrease Font Size" Click = "DecreaseFont_Click" />
</ContextMenu>
</TextBox.ContextMenu>
</TextBox>
</Grid>
</Window>
以下是不同事件的 **C# 实现**。
using System.Windows;
namespace WPFContextMenuControl {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
}
private void Bold_Checked(object sender, RoutedEventArgs e) {
textBox1.FontWeight = FontWeights.Bold;
}
private void Bold_Unchecked(object sender, RoutedEventArgs e) {
textBox1.FontWeight = FontWeights.Normal;
}
private void Italic_Checked(object sender, RoutedEventArgs e) {
textBox1.FontStyle = FontStyles.Italic;
}
private void Italic_Unchecked(object sender, RoutedEventArgs e) {
textBox1.FontStyle = FontStyles.Normal;
}
private void IncreaseFont_Click(object sender, RoutedEventArgs e) {
if (textBox1.FontSize < 18) {
textBox1.FontSize += 2;
}
}
private void DecreaseFont_Click(object sender, RoutedEventArgs e) {
if (textBox1.FontSize > 10) {
textBox1.FontSize -= 2;
}
}
}
}
编译并执行上述代码后,将生成以下窗口:
我们建议您执行上述示例代码,并尝试 ContextMenu 的其他一些属性和事件。