ContextMenuProvider 类
[本文档仅供预览,在以后的发行版中可能会发生更改。包含的空白主题用作占位符。]
定义在上下文菜单中显示的一组菜单项。
继承层次结构
System.Object
Microsoft.Windows.Design.Features.FeatureProvider
Microsoft.Windows.Design.Interaction.ContextMenuProvider
Microsoft.Windows.Design.Interaction.PrimarySelectionContextMenuProvider
命名空间: Microsoft.Windows.Design.Interaction
程序集: Microsoft.Windows.Design.Interaction(在 Microsoft.Windows.Design.Interaction.dll 中)
语法
声明
Public MustInherit Class ContextMenuProvider _
Inherits FeatureProvider
public abstract class ContextMenuProvider : FeatureProvider
public ref class ContextMenuProvider abstract : public FeatureProvider
[<AbstractClass>]
type ContextMenuProvider =
class
inherit FeatureProvider
end
public abstract class ContextMenuProvider extends FeatureProvider
ContextMenuProvider 类型公开以下成员。
构造函数
名称 | 说明 | |
---|---|---|
ContextMenuProvider | 初始化 ContextMenuProvider 类的新实例。 |
页首
属性
名称 | 说明 | |
---|---|---|
Items | 获取添加到上下文菜单中的项。 |
页首
方法
名称 | 说明 | |
---|---|---|
Equals | 确定指定的 Object 是否等于当前的 Object。 (继承自 Object。) | |
Finalize | 允许对象在“垃圾回收”回收之前尝试释放资源并执行其他清理操作。 (继承自 Object。) | |
GetHashCode | 用作特定类型的哈希函数。 (继承自 Object。) | |
GetType | 获取当前实例的 Type。 (继承自 Object。) | |
MemberwiseClone | 创建当前 Object 的浅表副本。 (继承自 Object。) | |
ToString | 返回表示当前对象的字符串。 (继承自 Object。) | |
Update | 引发 UpdateItemStatus 事件,以便能够刷新菜单项的可见性和启用状态。 |
页首
事件
名称 | 说明 | |
---|---|---|
UpdateItemStatus | 在将要显示菜单项时发生。 |
页首
备注
当您要将上下文菜单项添加到特定上下文菜单中时,从 ContextMenuProvider 类继承。 通过应用策略(如 PrimarySelectionPolicy 和 SelectionParentPolicy),可以控制创建上下文菜单提供程序的时间。
ContextMenuProvider 只临时存在。 不要假设它将会持续很长一段时间。
示例
下面的代码示例演示如何从 PrimarySelectionContextMenuProvider 类派生自定义类型。 派生类型实现自定义上下文菜单,可在设计时设置控件的 Background 的属性。 有关更多信息,请参见演练:创建菜单提供程序。
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports Microsoft.Windows.Design.Interaction
Imports System.Windows
Imports Microsoft.Windows.Design.Model
Imports System.Windows.Controls
Imports System.Windows.Media
' The CustomContextMenuProvider class provides two context menu items
' at design time. These are implemented with the MenuAction class.
Class CustomContextMenuProvider
Inherits PrimarySelectionContextMenuProvider
Private setBackgroundToBlueMenuAction As MenuAction
Private clearBackgroundMenuAction As MenuAction
' The provider's constructor sets up the MenuAction objects
' and the the MenuGroup which holds them.
Public Sub New()
' Set up the MenuAction which sets the control's
' background to Blue.
setBackgroundToBlueMenuAction = New MenuAction("Blue")
setBackgroundToBlueMenuAction.Checkable = True
AddHandler setBackgroundToBlueMenuAction.Execute, AddressOf SetBackgroundToBlue_Execute
' Set up the MenuAction which sets the control's
' background to its default value.
clearBackgroundMenuAction = New MenuAction("Cleared")
clearBackgroundMenuAction.Checkable = True
AddHandler clearBackgroundMenuAction.Execute, AddressOf ClearBackground_Execute
' Set up the MenuGroup which holds the MenuAction items.
Dim backgroundFlyoutGroup As New MenuGroup("SetBackgroundsGroup", "Set Background")
' If HasDropDown is false, the group appears inline,
' instead of as a flyout. Set to true.
backgroundFlyoutGroup.HasDropDown = True
backgroundFlyoutGroup.Items.Add(setBackgroundToBlueMenuAction)
backgroundFlyoutGroup.Items.Add(clearBackgroundMenuAction)
Me.Items.Add(backgroundFlyoutGroup)
' The UpdateItemStatus event is raised immediately before
' this provider shows its tabs, which provides the opportunity
' to set states.
AddHandler UpdateItemStatus, AddressOf CustomContextMenuProvider_UpdateItemStatus
End Sub
' The following method handles the UpdateItemStatus event.
' It sets the MenuAction states according to the state
' of the control's Background property. This method is
' called before the context menu is shown.
Sub CustomContextMenuProvider_UpdateItemStatus( _
ByVal sender As Object, _
ByVal e As MenuActionEventArgs)
' Turn everything on, and then based on the value
' of the BackgroundProperty, selectively turn some off.
clearBackgroundMenuAction.Checked = False
clearBackgroundMenuAction.Enabled = True
setBackgroundToBlueMenuAction.Checked = False
setBackgroundToBlueMenuAction.Enabled = True
' Get a ModelItem which represents the selected control.
Dim selectedControl As ModelItem = _
e.Selection.PrimarySelection
' Get the value of the Background property from the ModelItem.
Dim backgroundProperty As ModelProperty = _
selectedControl.Properties("Background")
' Set the MenuAction items appropriately.
If Not backgroundProperty.IsSet Then
clearBackgroundMenuAction.Checked = True
clearBackgroundMenuAction.Enabled = False
ElseIf backgroundProperty.ComputedValue.Equals(Brushes.Blue) Then
setBackgroundToBlueMenuAction.Checked = True
setBackgroundToBlueMenuAction.Enabled = False
End If
End Sub
' The following method handles the Execute event.
' It sets the Background property to its default value.
Sub ClearBackground_Execute( _
ByVal sender As Object, _
ByVal e As MenuActionEventArgs)
Dim selectedControl As ModelItem = e.Selection.PrimarySelection
selectedControl.Properties("Background").ClearValue()
End Sub
' The following method handles the Execute event.
' It sets the Background property to Brushes.Blue.
Sub SetBackgroundToBlue_Execute( _
ByVal sender As Object, _
ByVal e As MenuActionEventArgs)
Dim selectedControl As ModelItem = e.Selection.PrimarySelection
selectedControl.Properties("Background").SetValue(Brushes.Blue)
End Sub
End Class
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Windows.Design.Interaction;
using System.Windows;
using Microsoft.Windows.Design.Model;
using System.Windows.Controls;
using System.Windows.Media;
namespace CustomControlLibrary.VisualStudio.Design
{
// The CustomContextMenuProvider class provides two context menu items
// at design time. These are implemented with the MenuAction class.
class CustomContextMenuProvider : PrimarySelectionContextMenuProvider
{
private MenuAction setBackgroundToBlueMenuAction;
private MenuAction clearBackgroundMenuAction;
// The provider's constructor sets up the MenuAction objects
// and the the MenuGroup which holds them.
public CustomContextMenuProvider()
{
// Set up the MenuAction which sets the control's
// background to Blue.
setBackgroundToBlueMenuAction = new MenuAction("Blue");
setBackgroundToBlueMenuAction.Checkable = true;
setBackgroundToBlueMenuAction.Execute +=
new EventHandler<MenuActionEventArgs>(SetBackgroundToBlue_Execute);
// Set up the MenuAction which sets the control's
// background to its default value.
clearBackgroundMenuAction = new MenuAction("Cleared");
clearBackgroundMenuAction.Checkable = true;
clearBackgroundMenuAction.Execute +=
new EventHandler<MenuActionEventArgs>(ClearBackground_Execute);
// Set up the MenuGroup which holds the MenuAction items.
MenuGroup backgroundFlyoutGroup =
new MenuGroup("SetBackgroundsGroup", "Set Background");
// If HasDropDown is false, the group appears inline,
// instead of as a flyout. Set to true.
backgroundFlyoutGroup.HasDropDown = true;
backgroundFlyoutGroup.Items.Add(setBackgroundToBlueMenuAction);
backgroundFlyoutGroup.Items.Add(clearBackgroundMenuAction);
this.Items.Add(backgroundFlyoutGroup);
// The UpdateItemStatus event is raised immediately before
// this provider shows its tabs, which provides the opportunity
// to set states.
UpdateItemStatus +=
new EventHandler<MenuActionEventArgs>(
CustomContextMenuProvider_UpdateItemStatus);
}
// The following method handles the UpdateItemStatus event.
// It sets the MenuAction states according to the state
// of the control's Background property. This method is
// called before the context menu is shown.
void CustomContextMenuProvider_UpdateItemStatus(
object sender,
MenuActionEventArgs e)
{
// Turn everything on, and then based on the value
// of the BackgroundProperty, selectively turn some off.
clearBackgroundMenuAction.Checked = false;
clearBackgroundMenuAction.Enabled = true;
setBackgroundToBlueMenuAction.Checked = false;
setBackgroundToBlueMenuAction.Enabled = true;
// Get a ModelItem which represents the selected control.
ModelItem selectedControl = e.Selection.PrimarySelection;
// Get the value of the Background property from the ModelItem.
ModelProperty backgroundProperty =
selectedControl.Properties["Background"];
// Set the MenuAction items appropriately.
if (!backgroundProperty.IsSet)
{
clearBackgroundMenuAction.Checked = true;
clearBackgroundMenuAction.Enabled = false;
}
else if (backgroundProperty.ComputedValue == Brushes.Blue)
{
setBackgroundToBlueMenuAction.Checked = true;
setBackgroundToBlueMenuAction.Enabled = false;
}
}
// The following method handles the Execute event.
// It sets the Background property to its default value.
void ClearBackground_Execute(
object sender,
MenuActionEventArgs e)
{
ModelItem selectedControl = e.Selection.PrimarySelection;
selectedControl.Properties["Background"].ClearValue();
}
// The following method handles the Execute event.
// It sets the Background property to Brushes.Blue.
void SetBackgroundToBlue_Execute(
object sender,
MenuActionEventArgs e)
{
ModelItem selectedControl = e.Selection.PrimarySelection;
selectedControl.Properties["Background"].SetValue(Brushes.Blue);
}
}
}
线程安全
此类型的任何公共 static(在 Visual Basic 中为 Shared) 成员都是线程安全的。但不保证所有实例成员都是线程安全的。
请参见
参考
Microsoft.Windows.Design.Interaction 命名空间
PrimarySelectionContextMenuProvider