AdornerProvider.Activate 方法
會在設計工具第一次要求裝飾項時呼叫。
命名空間: Microsoft.Windows.Design.Interaction
組件: Microsoft.Windows.Design.Interaction (在 Microsoft.Windows.Design.Interaction.dll 中)
語法
'宣告
Protected Overridable Sub Activate ( _
item As ModelItem _
)
protected virtual void Activate(
ModelItem item
)
protected:
virtual void Activate(
ModelItem^ item
)
abstract Activate :
item:ModelItem -> unit
override Activate :
item:ModelItem -> unit
protected function Activate(
item : ModelItem
)
參數
- item
型別:Microsoft.Windows.Design.Model.ModelItem
ModelItem ,表示所裝飾的項目。
備註
裝飾項提供者可能會使用所裝飾的項目 (Element Item) 或檢視來初始化裝飾項。 如果不需要這個內容,裝飾項提供者就能在建構函式中建立這些裝飾項,而不需多載 Activate 方法。 這個方法是在裝飾項成為設計工具 UI 的父代之前呼叫。
AdornerProvider 執行個體可在其存留期 (Lifetime) 啟動和停用數次。 依此實作 Activate 和 Deactivate 方法。
範例
在下列程式碼範例中,會說明如何覆寫 Activate 方法以建立 AdornerPanel 來裝載 Slider 控制項,這個控制項是在設計階段用來設定當做裝飾項之控制項的 Background 屬性。 如需詳細資訊,請參閱逐步解說:建立設計階段裝飾項。
' 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 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;
}
}
.NET Framework 安全性
- 完全信任立即呼叫者。這個成員無法供部分信任的程式碼使用。如需詳細資訊,請參閱從部分受信任程式碼使用程式庫。
請參閱
參考
Microsoft.Windows.Design.Interaction 命名空間