PrimarySelectionContextMenuProvider 類別
更新:2007 年 11 月
提供一組 MenuGroup 項目,供目前選項使用。
命名空間: Microsoft.Windows.Design.Interaction
組件: Microsoft.Windows.Design.Extensibility (在 Microsoft.Windows.Design.Extensibility.dll 中)
語法
<UsesItemPolicyAttribute(GetType(PrimarySelectionPolicy))> _
Public MustInherit Class PrimarySelectionContextMenuProvider _
Inherits ContextMenuProvider
Dim instance As PrimarySelectionContextMenuProvider
[UsesItemPolicyAttribute(typeof(PrimarySelectionPolicy))]
public abstract class PrimarySelectionContextMenuProvider : ContextMenuProvider
[UsesItemPolicyAttribute(typeof(PrimarySelectionPolicy))]
public ref class PrimarySelectionContextMenuProvider abstract : public ContextMenuProvider
public abstract class PrimarySelectionContextMenuProvider extends ContextMenuProvider
備註
您可以繼承自 PrimarySelectionContextMenuProvider 類別,以便加入當控制項在設計介面上遭選取時,會顯示的內容功能表項目。您必須處理 UpdateItemStatus 事件,以便在內容功能表顯示之前將 MenuAction 項目設定完成。
範例
在下列程式碼範例中,會說明如何從 PrimarySelectionContextMenuProvider 類別衍生自訂型別。衍生的型別會實作自訂內容功能表,會在設計階段設定控制項的 Background 屬性。如需詳細資訊,請參閱逐步解說:建立 MenuAction。
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(Control.BackgroundProperty)
' 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(Control.BackgroundProperty).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(Control.BackgroundProperty).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 SliderAdornerLib
{
// 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[Control.BackgroundProperty];
// 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[Control.BackgroundProperty].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[Control.BackgroundProperty].SetValue(Brushes.Blue);
}
}
}
繼承階層架構
System.Object
Microsoft.Windows.Design.Features.FeatureProvider
Microsoft.Windows.Design.Interaction.ContextMenuProvider
Microsoft.Windows.Design.Interaction.PrimarySelectionContextMenuProvider
執行緒安全
這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。
請參閱
參考
PrimarySelectionContextMenuProvider 成員