XAML - ContextMenu
ContextMenu 表示一个弹出菜单,使控件能够公开特定于控件上下文的功能。每当通过此元素内的用户界面请求上下文菜单时,它都会出现。ContextMenu 类的层次继承如下 −
属性
| Sr.No. | 属性 &描述 |
|---|---|
| 1 | Background 获取或设置提供控件背景的画笔。(从控件继承) |
| 2 | BorderThickness 获取或设置控件的边框厚度。(从控件继承) |
| 3 | ContextMenu 获取或设置当通过用户界面 (UI) 从此元素内请求上下文菜单时应出现的上下文菜单元素。 (从 FrameworkElement 继承。) |
| 4 | FontFamily 获取或设置用于在控件中显示文本的字体。(从控件继承) |
| 5 | FontSize 获取或设置此控件中文本的大小。(从控件继承) |
| 6 | FontStyle 获取或设置呈现文本的样式。 (从控件继承) |
| 7 | FontWeight 获取或设置指定字体的粗细。 (从控件继承) |
| 8 | Foreground 获取或设置描述前景色的画笔。 (从控件继承) |
| 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 获取或设置在布局和渲染期间应用于此对象的实例 Style。 (从 FrameworkElement 继承) |
| 21 | VerticalAlignment 获取或设置在父对象(例如面板或项目控件)中组合 FrameworkElement 时应用于它的垂直对齐特征。 (从 FrameworkElement 继承) |
| 22 | Width 获取或设置 FrameworkElement 的宽度。(从 FrameworkElement 继承) |
方法
| Sr.No. | 方法和说明 |
|---|---|
| 1 | AddChild 将指定对象添加为 ItemsControl 对象的子对象。 (从 ItemsControl 继承。) |
| 2 | Arrange 定位子对象并确定 UIElement 的大小。为其子元素实现自定义布局的父对象应从其布局覆盖实现中调用此方法以形成递归布局更新。(从 UIElement 继承) |
| 3 | FindName 检索具有指定标识符名称的对象。(从 FrameworkElement 继承) |
| 4 | Focus 尝试将焦点设置在控件上。 (从 Control 继承) |
| 5 | GetValue 从 DependencyObject 返回依赖项属性的当前有效值。 (从 DependencyObject 继承) |
| 6 | IsItemItsOwnContainer 确定指定项是否(或是否有资格成为)其自己的容器。 (从 ItemsControl 继承。) |
| 7 | OnDragEnter 在 DragEnter 事件发生之前调用。 (从控件继承) |
| 8 | OnDragLeave 在 DragLeave 事件发生之前调用。 (从控件继承) |
| 9 | OnDragOver 在 DragOver 事件发生之前调用。 (从控件继承) |
| 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 继承) |
事件
| Sr.No. | 事件和说明 |
|---|---|
| 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) |
示例
以下示例包含一个文本框,其中有一个 ContextMenu,用于操作文本框内的文本。
以下是 XAML 代码,其中创建了一个 TextBox、一个 ContextMenu 和 MenuItem,并附带一些属性和事件。
<Window x:Class = "XAMLContextMenu.MainWindow"
xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x = "http://schemas.microsoft.com/winfx/2006/xaml"
Title = "MainWindow" Height = "350" Width = "604">
<Grid>
<TextBox Name = "textBox1" TextWrapping = "Wrap" Margin = "10" Grid.Row = "7">
Hi, this is XAML tutorial.
<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;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
namespace XAMLContextMenu {
/// <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;
}
}
}
}
当你编译并执行上面的代码时,它会产生以下屏幕−
我们建议您执行上面的示例代码并尝试一些其他属性和事件。

xaml_controls.html