AdornerProvider.Activate 方法

更新:2007 年 11 月

在设计器初次请求装饰器时调用。

命名空间:  Microsoft.Windows.Design.Interaction
程序集:  Microsoft.Windows.Design.Extensibility(在 Microsoft.Windows.Design.Extensibility.dll 中)

语法

声明
Protected Overridable Sub Activate ( _
    item As ModelItem, _
    view As DependencyObject _
)
用法
Dim item As ModelItem
Dim view As DependencyObject

Me.Activate(item, view)
protected virtual void Activate(
    ModelItem item,
    DependencyObject view
)
protected:
virtual void Activate(
    ModelItem^ item, 
    DependencyObject^ view
)
protected function Activate(
    item : ModelItem, 
    view : DependencyObject
)

参数

异常

异常 条件
ArgumentNullException

view 为 nullnull 引用(在 Visual Basic 中为 Nothing)。

备注

装饰器提供程序可能使用所装饰的元素项或视图来初始化装饰器。如果不需要此上下文,装饰器提供程序便可在构造函数中创建装饰器,而无需重载 Activate 方法。在装饰器成为设计器 UI 的父项之前调用此方法。

一个 AdornerProvider 实例在其生存期内可以激活和停用多次。相应地实现 Activate 和 Deactivate 方法。

示例

下面的代码示例演示如何重写 Activate 方法以创建用于承载 Slider 控件的 AdornerPanel,该控件在设计时用来设置所装饰的控件的 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, ByVal view As DependencyObject)

    ' 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

    AdornerPanel.SetHorizontalStretch(opacitySlider, AdornerStretch.Stretch)
    AdornerPanel.SetVerticalStretch(opacitySlider, AdornerStretch.None)

    Dim placement As New AdornerPlacementCollection()

    ' The adorner's width is relative to the content.
    ' The slider extends the full width of the control it adorns.
    placement.SizeRelativeToContentWidth(1.0, 0)

    ' The adorner's height is the same as the slider's.
    placement.SizeRelativeToAdornerDesiredHeight(1.0, 0)

    ' Position the adorner above the control it adorns.
    placement.PositionRelativeToAdornerHeight(-1.0, 0)

    ' Position the adorner up 5 pixels. This demonstrates 
    ' that these placement calls are additive. These two calls
    ' are equivalent to the following single call:
    ' PositionRelativeToAdornerHeight(-1.0, -5).
    placement.PositionRelativeToAdornerHeight(0, -5)

    AdornerPanel.SetPlacements(opacitySlider, placement)

    ' 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, view)

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, DependencyObject view)
{
    // 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;

    AdornerPanel.SetHorizontalStretch(opacitySlider, AdornerStretch.Stretch);
    AdornerPanel.SetVerticalStretch(opacitySlider, AdornerStretch.None);

    AdornerPlacementCollection placement = new AdornerPlacementCollection();

    // The adorner's width is relative to the content.
    // The slider extends the full width of the control it adorns.
    placement.SizeRelativeToContentWidth(1.0, 0);

    // The adorner's height is the same as the slider's.
    placement.SizeRelativeToAdornerDesiredHeight(1.0, 0);

    // Position the adorner above the control it adorns.
    placement.PositionRelativeToAdornerHeight(-1.0, 0);

    // Position the adorner up 5 pixels. This demonstrates 
    // that these placement calls are additive. These two calls
    // are equivalent to the following single call:
    // PositionRelativeToAdornerHeight(-1.0, -5).
    placement.PositionRelativeToAdornerHeight(0, -5);

    AdornerPanel.SetPlacements(opacitySlider, placement);

    // 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, view);
}

// 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;
    } 
}

权限

另请参见

参考

AdornerProvider 类

AdornerProvider 成员

Microsoft.Windows.Design.Interaction 命名空间

其他资源

装饰器体系结构

布局空间和呈现空间

功能提供程序和功能连接器

演练:创建设计时装饰器