AdornerProvider 類別
您可以將其加入至類別,以便自動將裝飾項加入至設計工具介面的功能提供者。
繼承階層架構
System.Object
Microsoft.Windows.Design.Features.FeatureProvider
Microsoft.Windows.Design.Interaction.AdornerProvider
Microsoft.Windows.Design.Interaction.PrimarySelectionAdornerProvider
命名空間: Microsoft.Windows.Design.Interaction
組件: Microsoft.Windows.Design.Interaction (在 Microsoft.Windows.Design.Interaction.dll 中)
語法
'宣告
Public MustInherit Class AdornerProvider _
Inherits FeatureProvider
public abstract class AdornerProvider : FeatureProvider
public ref class AdornerProvider abstract : public FeatureProvider
[<AbstractClass>]
type AdornerProvider =
class
inherit FeatureProvider
end
public abstract class AdornerProvider extends FeatureProvider
AdornerProvider 型別會公開下列成員。
建構函式
名稱 | 說明 | |
---|---|---|
AdornerProvider | 初始化 AdornerProvider 類別的新執行個體。 |
回頁首
屬性
名稱 | 說明 | |
---|---|---|
Adorners | 取得這個裝飾項提供者所提供的裝飾項集合。 | |
AdornersVisible | 取得或設定值,這個值表示是否能看見一組裝飾項。 | |
Context | 取得啟動這個裝飾項提供者的編輯內容。 |
回頁首
方法
名稱 | 說明 | |
---|---|---|
Activate | 會在設計工具第一次要求裝飾項時呼叫。 | |
Deactivate | 會在設計工具即將捨棄裝飾項提供者時呼叫。 | |
Equals | 判斷指定的 Object 和目前的 Object 是否相等。 (繼承自 Object)。 | |
Finalize | 允許物件在記憶體回收進行回收之前,嘗試釋放資源並執行其他清除作業。 (繼承自 Object)。 | |
GetHashCode | 做為特定型別的雜湊函式。 (繼承自 Object)。 | |
GetType | 取得目前執行個體的 Type。 (繼承自 Object)。 | |
IsToolSupported | 取得值,表示裝飾項提供者是否支援所提供的 Tool。 | |
MemberwiseClone | 建立目前 Object 的淺層複本 (Shallow Copy)。 (繼承自 Object)。 | |
ToString | 傳回表示目前物件的字串。 (繼承自 Object)。 |
回頁首
備註
加入裝飾項時是根據 AdornerProvider 的原則來判斷。 您可以將 UsesItemPolicyAttribute 加入至類別定義,以便將原則加入至 AdornerProvider。
若要加入選取期間會出現的裝飾項,請衍生自 PrimarySelectionAdornerProvider,因為它已經套用了正確的原則屬性 (Attribute)。
範例
在下列程式碼範例中,會說明如何從 PrimarySelectionAdornerProvider 類別衍生自訂型別。 這個自訂型別會建立 AdornerPanel 以裝載 Slider 控制項,這個控制項是用來在設計階段設定所裝飾控制項的 Background 屬性。 如需詳細資訊,請參閱逐步解說:建立設計階段裝飾項。
Imports System
Imports System.Collections.Generic
Imports System.Text
Imports System.Windows.Input
Imports System.Windows
Imports System.Windows.Automation
Imports System.Windows.Controls
Imports System.Windows.Media
Imports System.Windows.Shapes
Imports Microsoft.Windows.Design.Interaction
Imports Microsoft.Windows.Design.Model
Namespace CustomControlLibrary.VisualStudio.Design
' The following class implements an adorner provider for the
' adorned control. The adorner is a slider control, which
' changes the Background opacity of the adorned control.
Class OpacitySliderAdornerProvider
Inherits PrimarySelectionAdornerProvider
Private adornedControlModel As ModelItem
Private batchedChange As ModelEditingScope
Private opacitySlider As Slider
Private opacitySliderAdornerPanel As AdornerPanel
Public Sub New()
opacitySlider = New Slider()
End Sub
' The following method is called when the adorner is activated.
' It creates the adorner control, sets up the adorner panel,
' and attaches a ModelItem to the adorned control.
Protected Overrides Sub Activate(ByVal item As ModelItem)
' Save the ModelItem and hook into when it changes.
' This enables updating the slider position when
' a new Background value is set.
adornedControlModel = item
AddHandler adornedControlModel.PropertyChanged, AddressOf AdornedControlModel_PropertyChanged
' Setup the slider's min and max values.
opacitySlider.Minimum = 0
opacitySlider.Maximum = 1
' Setup the adorner panel.
' All adorners are placed in an AdornerPanel
' for sizing and layout support.
Dim myPanel = Me.Panel
' The slider extends the full width of the control it adorns.
AdornerPanel.SetAdornerHorizontalAlignment( _
opacitySlider, _
AdornerHorizontalAlignment.Stretch)
' Position the adorner above the control it adorns.
AdornerPanel.SetAdornerVerticalAlignment( _
opacitySlider, _
AdornerVerticalAlignment.OutsideTop)
' Position the adorner 5 pixels above the control.
AdornerPanel.SetAdornerMargin( _
opacitySlider, _
New Thickness(0, 0, 0, 5))
' Initialize the slider when it is loaded.
AddHandler opacitySlider.Loaded, AddressOf slider_Loaded
' Handle the value changes of the slider control.
AddHandler opacitySlider.ValueChanged, AddressOf slider_ValueChanged
AddHandler opacitySlider.PreviewMouseLeftButtonUp, _
AddressOf slider_MouseLeftButtonUp
AddHandler opacitySlider.PreviewMouseLeftButtonDown, _
AddressOf slider_MouseLeftButtonDown
MyBase.Activate(item)
End Sub
' The Panel utility property demand-creates the
' adorner panel and adds it to the provider's
' Adorners collection.
Public ReadOnly Property Panel() As AdornerPanel
Get
If Me.opacitySliderAdornerPanel Is Nothing Then
Me.opacitySliderAdornerPanel = New AdornerPanel()
' Add the adorner to the adorner panel.
Me.opacitySliderAdornerPanel.Children.Add(opacitySlider)
' Add the panel to the Adorners collection.
Adorners.Add(opacitySliderAdornerPanel)
End If
Return Me.opacitySliderAdornerPanel
End Get
End Property
' The following method deactivates the adorner.
Protected Overrides Sub Deactivate()
RemoveHandler adornedControlModel.PropertyChanged, _
AddressOf AdornedControlModel_PropertyChanged
MyBase.Deactivate()
End Sub
' The following method handles the PropertyChanged event.
' It updates the slider control's value if the adorned control's
' Background property changed,
Sub AdornedControlModel_PropertyChanged( _
ByVal sender As Object, _
ByVal e As System.ComponentModel.PropertyChangedEventArgs)
If e.PropertyName = "Background" Then
opacitySlider.Value = GetCurrentOpacity()
End If
End Sub
' The following method handles the Loaded event.
' It assigns the slider control's initial value.
Sub slider_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
opacitySlider.Value = GetCurrentOpacity()
End Sub
' The following method handles the MouseLeftButtonDown event.
' It calls the BeginEdit method on the ModelItem which represents
' the adorned control.
Sub slider_MouseLeftButtonDown( _
ByVal sender As Object, _
ByVal e As System.Windows.Input.MouseButtonEventArgs)
batchedChange = adornedControlModel.BeginEdit()
End Sub
' The following method handles the MouseLeftButtonUp event.
' It commits any changes made to the ModelItem which represents the
' the adorned control.
Sub slider_MouseLeftButtonUp( _
ByVal sender As Object, _
ByVal e As System.Windows.Input.MouseButtonEventArgs)
If Not (batchedChange Is Nothing) Then
batchedChange.Complete()
batchedChange.Dispose()
batchedChange = Nothing
End If
End Sub
' The following method handles the slider control's
' ValueChanged event. It sets the value of the
' Background opacity by using the ModelProperty type.
Sub slider_ValueChanged( _
ByVal sender As Object, _
ByVal e As RoutedPropertyChangedEventArgs(Of Double))
If (True) Then
Dim newOpacityValue As Double = e.NewValue
' During setup, don't make a value local and set the opacity.
If newOpacityValue = GetCurrentOpacity() Then
Return
End If
' Access the adorned control's Background property
' by using the ModelProperty type.
Dim backgroundProperty As ModelProperty = _
adornedControlModel.Properties("Background")
If Not backgroundProperty.IsSet Then
' If the value isn't local, make it local
' before setting a sub-property value.
backgroundProperty.SetValue(backgroundProperty.ComputedValue)
End If
' Set the Opacity property on the Background Brush.
backgroundProperty.Value.Properties("Opacity").SetValue(newOpacityValue)
End If
End Sub
' This utility method gets the adorned control's
' Background brush by using the ModelItem.
Function GetCurrentOpacity() As Double
Dim backgroundBrushComputedValue As Brush = _
CType(adornedControlModel.Properties("Background").ComputedValue, _
Brush)
Return backgroundBrushComputedValue.Opacity
End Function
End Class
End Namespace
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Shapes;
using Microsoft.Windows.Design.Interaction;
using Microsoft.Windows.Design.Model;
namespace CustomControlLibrary.VisualStudio.Design
{
// The following class implements an adorner provider for the
// adorned control. The adorner is a slider control, which
// changes the Background opacity of the adorned control.
class OpacitySliderAdornerProvider : PrimarySelectionAdornerProvider
{
private ModelItem adornedControlModel;
private ModelEditingScope batchedChange;
private Slider opacitySlider;
private AdornerPanel opacitySliderAdornerPanel;
public OpacitySliderAdornerProvider()
{
opacitySlider = new Slider();
}
// The following method is called when the adorner is activated.
// It creates the adorner control, sets up the adorner panel,
// and attaches a ModelItem to the adorned control.
protected override void Activate(ModelItem item)
{
// Save the ModelItem and hook into when it changes.
// This enables updating the slider position when
// a new Background value is set.
adornedControlModel = item;
adornedControlModel.PropertyChanged +=
new System.ComponentModel.PropertyChangedEventHandler(
AdornedControlModel_PropertyChanged);
// Setup the slider's min and max values.
opacitySlider.Minimum = 0;
opacitySlider.Maximum = 1;
// Setup the adorner panel.
// All adorners are placed in an AdornerPanel
// for sizing and layout support.
AdornerPanel myPanel = this.Panel;
// The slider extends the full width of the control it adorns.
AdornerPanel.SetAdornerHorizontalAlignment(
opacitySlider,
AdornerHorizontalAlignment.Stretch);
// Position the adorner above the control it adorns.
AdornerPanel.SetAdornerVerticalAlignment(
opacitySlider,
AdornerVerticalAlignment.OutsideTop);
// Position the adorner 5 pixels above the control.
AdornerPanel.SetAdornerMargin(
opacitySlider,
new Thickness(0, 0, 0, 5));
// Initialize the slider when it is loaded.
opacitySlider.Loaded += new RoutedEventHandler(slider_Loaded);
// Handle the value changes of the slider control.
opacitySlider.ValueChanged +=
new RoutedPropertyChangedEventHandler<double>(
slider_ValueChanged);
opacitySlider.PreviewMouseLeftButtonUp +=
new System.Windows.Input.MouseButtonEventHandler(
slider_MouseLeftButtonUp);
opacitySlider.PreviewMouseLeftButtonDown +=
new System.Windows.Input.MouseButtonEventHandler(
slider_MouseLeftButtonDown);
base.Activate(item);
}
// The Panel utility property demand-creates the
// adorner panel and adds it to the provider's
// Adorners collection.
public AdornerPanel Panel
{
get
{
if (this.opacitySliderAdornerPanel == null)
{
opacitySliderAdornerPanel = new AdornerPanel();
opacitySliderAdornerPanel.Children.Add(opacitySlider);
// Add the panel to the Adorners collection.
Adorners.Add(opacitySliderAdornerPanel);
}
return this.opacitySliderAdornerPanel;
}
}
// The following method deactivates the adorner.
protected override void Deactivate()
{
adornedControlModel.PropertyChanged -=
new System.ComponentModel.PropertyChangedEventHandler(
AdornedControlModel_PropertyChanged);
base.Deactivate();
}
// The following method handles the PropertyChanged event.
// It updates the slider control's value if the adorned control's
// Background property changed,
void AdornedControlModel_PropertyChanged(
object sender,
System.ComponentModel.PropertyChangedEventArgs e)
{
if (e.PropertyName == "Background")
{
opacitySlider.Value = GetCurrentOpacity();
}
}
// The following method handles the Loaded event.
// It assigns the slider control's initial value.
void slider_Loaded(object sender, RoutedEventArgs e)
{
opacitySlider.Value = GetCurrentOpacity();
}
// The following method handles the MouseLeftButtonDown event.
// It calls the BeginEdit method on the ModelItem which represents
// the adorned control.
void slider_MouseLeftButtonDown(
object sender,
System.Windows.Input.MouseButtonEventArgs e)
{
batchedChange = adornedControlModel.BeginEdit();
}
// The following method handles the MouseLeftButtonUp event.
// It commits any changes made to the ModelItem which represents the
// the adorned control.
void slider_MouseLeftButtonUp(
object sender,
System.Windows.Input.MouseButtonEventArgs e)
{
if (batchedChange != null)
{
batchedChange.Complete();
batchedChange.Dispose();
batchedChange = null;
}
}
// The following method handles the slider control's
// ValueChanged event. It sets the value of the
// Background opacity by using the ModelProperty type.
void slider_ValueChanged(
object sender,
RoutedPropertyChangedEventArgs<double> e)
{
double newOpacityValue = e.NewValue;
// During setup, don't make a value local and set the opacity.
if (newOpacityValue == GetCurrentOpacity())
{
return;
}
// Access the adorned control's Background property
// by using the ModelProperty type.
ModelProperty backgroundProperty =
adornedControlModel.Properties["Background"];
if (!backgroundProperty.IsSet)
{
// If the value isn't local, make it local
// before setting a sub-property value.
backgroundProperty.SetValue(backgroundProperty.ComputedValue);
}
// Set the Opacity property on the Background Brush.
backgroundProperty.Value.Properties["Opacity"].SetValue(newOpacityValue);
}
// This utility method gets the adorned control's
// Background brush by using the ModelItem.
private double GetCurrentOpacity()
{
Brush backgroundBrushComputedValue =
(Brush)adornedControlModel.Properties["Background"].ComputedValue;
return backgroundBrushComputedValue.Opacity;
}
}
}
執行緒安全
這個型別的任何 Public static (在 Visual Basic 中為 Shared) 成員都具備執行緒安全。並非所有的執行個體成員都是安全執行緒。
請參閱
參考
Microsoft.Windows.Design.Interaction 命名空間