AdornerProvider Class

A feature provider that you can add to a class to automatically add adorners to the designer surface.

Namespace:  Microsoft.Windows.Design.Interaction
Assembly:  Microsoft.Windows.Design.Interaction (in Microsoft.Windows.Design.Interaction.dll)

public abstract class AdornerProvider : FeatureProvider

The AdornerProvider type exposes the following members.

Protected methodAdornerProviderInitializes a new instance of the AdornerProvider class.

Public propertyAdornersGets the collection of adorners that are provided by this adorner provider.
Public propertyAdornersVisibleGets or sets the visibility of a set of adorners.
Protected propertyContextGets the editing context that activated this adorner provider.

Protected methodActivateCalled when adorners are requested for the first time by the designer.
Protected methodDeactivateCalled when an adorner provider is about to be discarded by the designer.
Public methodEqualsDetermines whether the specified Object is equal to the current Object. (Inherited from Object.)
Protected methodFinalizeAllows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public methodGetHashCodeServes as a hash function for a particular type. (Inherited from Object.)
Public methodGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsToolSupportedGets a value indicating if the adorner provider supports the provided Tool.
Protected methodMemberwiseCloneCreates a shallow copy of the current Object. (Inherited from Object.)
Public methodToStringReturns a string that represents the current object. (Inherited from Object.)

Adorners are added according to an AdornerProvider's policy. You can add a policy to an AdornerProvider by adding a UsesItemPolicyAttribute to the class definition.

To add adorners that appear during selection, derive from PrimarySelectionAdornerProvider as it already has the correct policy attribute applied.

The following code example shows how to derive a custom type from the PrimarySelectionAdornerProvider class. The custom type creates an AdornerPanel to host a Slider control, which is used at design-time to set the Background property of the adorned control. For more information, see Walkthrough: Creating a Design-time Adorner.

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(

            // 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.

            // Position the adorner above the control it adorns.

            // Position the adorner 5 pixels above the control. 
                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>(

            opacitySlider.PreviewMouseLeftButtonUp += 
                new System.Windows.Input.MouseButtonEventHandler(

            opacitySlider.PreviewMouseLeftButtonDown += 
                new System.Windows.Input.MouseButtonEventHandler(


        // The Panel utility property demand-creates the 
        // adorner panel and adds it to the provider's 
        // Adorners collection.
        public AdornerPanel Panel 
                if (this.opacitySliderAdornerPanel == null)
                    opacitySliderAdornerPanel = new AdornerPanel();


                    // Add the panel to the Adorners collection.

                return this.opacitySliderAdornerPanel;

        // The following method deactivates the adorner.
        protected override void Deactivate()
            adornedControlModel.PropertyChanged -= 
                new System.ComponentModel.PropertyChangedEventHandler(

        // 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 = 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())

            // Access the adorned control's Background property
            // by using the ModelProperty type.
            ModelProperty backgroundProperty = 
            if (!backgroundProperty.IsSet)
                // If the value isn't local, make it local 
                // before setting a sub-property value.

            // Set the Opacity property on the Background Brush.

        // This utility method gets the adorned control's
        // Background brush by using the ModelItem.
        private double GetCurrentOpacity()
            Brush backgroundBrushComputedValue =

            return backgroundBrushComputedValue.Opacity;

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions