Exemplarische Vorgehensweise: Erstellen eines Entwurfszeitadorners

In dieser exemplarischen Vorgehensweise wird veranschaulicht, wie Sie einen Entwurfszeitadorner für ein benutzerdefiniertes WPF (Windows Presentation Foundation)-Steuerelement erstellen. Mit diesem Adorner können Sie im WPF Designer für Visual Studio für ein benutzerdefiniertes Schaltflächen-Steuerelement den Wert der Opacity-Eigenschaft festlegen. Bei dem Steuerelement in dieser exemplarischen Vorgehensweise handelt es sich um eine einfache Schaltfläche, und der Adorner ist ein Schieberegler, mit dem Sie die Durchlässigkeit der Schaltfläche ändern können. Eine vollständige Codeauflistung finden Sie im Beispiel zum Schieberegleradorner für die Deckkraft auf der Website mit den Beispielen für WPF-Designer-Erweiterbarkeit.

Im Verlauf dieser exemplarischen Vorgehensweise führen Sie folgende Aufgaben aus:

  • Erstellen eines benutzerdefinierten WPF-Steuerelementbibliothek-Projekts

  • Erstellen einer separaten Assembly für Entwurfszeitmetadaten

  • Implementieren des Adorneranbieters

  • Testen des Steuerelements zur Entwurfszeit

Nach Abschluss dieser Aufgaben wissen Sie, wie Sie einen Adorneranbieter für ein benutzerdefiniertes Steuerelement erstellen.

Tipp

Je nach den aktiven Einstellungen oder der Version unterscheiden sich die Dialogfelder und Menübefehle auf Ihrem Bildschirm möglicherweise von den in der Hilfe beschriebenen. Klicken Sie im Menü Extras auf Einstellungen importieren und exportieren, um die Einstellungen zu ändern. Weitere Informationen finden Sie unter Arbeiten mit Einstellungen.

Vorbereitungsmaßnahmen

Zum Durchführen dieser exemplarischen Vorgehensweise benötigen Sie die folgenden Komponenten:

  • Visual Studio 2010.

Erstellen des benutzerdefinierten Steuerelements

Zuerst wird das Projekt für das benutzerdefinierte Steuerelement erstellt. Bei dem Steuerelement handelt es sich um eine einfache Schaltfläche mit wenig Entwurfszeitcode, für die eine GetIsInDesignMode-Methode zum Implementieren eines Entwurfszeitverhaltens verwendet wird.

So erstellen Sie das benutzerdefinierte Steuerelement

  1. Erstellen Sie ein neues benutzerdefiniertes WPF-Steuerelementbibliothek-Projekt in Visual Basic oder Visual C# mit dem Namen CustomControlLibrary.

    Der Code für CustomControl1 wird im Code-Editor geöffnet.

  2. Ändern Sie im Projektmappen-Explorer den Namen der Codedatei in ButtonWithDesignTime.cs oder ButtonWithDesignTime.vb. Wenn eine Meldung mit der Frage angezeigt wird, ob Sie eine Umbenennung für alle Verweise in diesem Projekt vornehmen möchten, klicken Sie auf Ja.

  3. Öffnen Sie ButtonWithDesignTime.cs bzw. ButtonWithDesignTime.vb im Code-Editor.

  4. Ersetzen Sie den automatisch generierten Code durch den folgenden Code. Das benutzerdefinierte ButtonWithDesignTime-Steuerelement erbt von Button und zeigt den Text "Aktiver Entwurfsmodus" an, wenn die Schaltfläche im Designer angezeigt wird. Die GetIsInDesignMode-Überprüfung und der folgende Entwurfszeitcode sind optional und werden ausschließlich zu Demonstrationszwecken gezeigt.

    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.Windows.Controls
    Imports System.Windows.Media
    Imports System.ComponentModel
    
    Public Class ButtonWithDesignTime
        Inherits Button
    
        Public Sub New()
            ' The GetIsInDesignMode check and the following design-time 
            ' code are optional and shown only for demonstration.
            If DesignerProperties.GetIsInDesignMode(Me) Then
                Content = "Design mode active"
            End If
    
        End Sub
    End Class
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Windows.Controls;
    using System.Windows.Media;
    using System.ComponentModel;
    
    namespace CustomControlLibrary
    {
        public class ButtonWithDesignTime : Button
        {
            public ButtonWithDesignTime()
            {
                // The GetIsInDesignMode check and the following design-time 
                // code are optional and shown only for demonstration.
                if (DesignerProperties.GetIsInDesignMode(this))
                {
                    Content = "Design mode active";
                }
            }
        }
    }
    
  5. Legen Sie den Ausgabepfad des Projekts auf "bin\" fest.

  6. Erstellen Sie die Projektmappe.

Erstellen der Entwurfszeit-Metadatenassembly

Entwurfszeitcode wird in speziellen Metadatenassemblys bereitgestellt. Bei dieser exemplarischen Vorgehensweise wird der benutzerdefinierte Adorner nur von Visual Studio unterstützt und in einer Assembly mit dem Namen CustomControlLibrary.VisualStudio.Design bereitgestellt. Weitere Informationen finden Sie unter Bereitstellen von Entwurfszeitmetadaten.

So erstellen Sie die Entwurfszeit-Metadatenassembly

  1. Fügen Sie der Projektmappe ein neues Klassenbibliothek-Projekt in Visual Basic oder Visual C# mit dem Namen CustomControlLibrary.VisualStudio.Design hinzu.

  2. Legen Sie den Ausgabepfad des Projekts auf folgenden Pfad fest: ".. \CustomControlLibrary\bin\". Dadurch wird die Assembly des Steuerelements im selben Ordner wie die Metadatenassembly gespeichert, wodurch Designern die Metadatensuche ermöglicht wird.

  3. Fügen Sie Verweise auf die folgenden WPF-Assemblys hinzu.

    • PresentationCore

    • PresentationFramework

    • System.Xaml

    • WindowsBase

  4. Fügen Sie Verweise auf die folgenden WPF-Designer-Assemblys hinzu.

    • Microsoft.Windows.Design.Extensibility

    • Microsoft.Windows.Design.Interaction

  5. Fügen Sie einen Verweis auf das CustomControlLibrary-Projekt hinzu.

  6. Ändern Sie im Projektmappen-Explorer den Namen der Class1-Codedatei in Metadata.cs oder Metadata.vb.

  7. Ersetzen Sie den automatisch generierten Code durch den folgenden Code. Durch diesen Code wird eine AttributeTable erstellt, mit der die benutzerdefinierte Entwurfszeitimplementierung an die ButtonWithDesignTime-Klasse angefügt wird.

    Imports System
    Imports System.Collections.Generic
    Imports System.Text
    Imports System.ComponentModel
    Imports System.Windows.Media
    Imports System.Windows.Controls
    Imports System.Windows
    Imports CustomControlLibrary
    Imports Microsoft.Windows.Design.Features
    Imports Microsoft.Windows.Design.Metadata
    
    ' The ProvideMetadata assembly-level attribute indicates to designers
    ' that this assembly contains a class that provides an attribute table. 
    <Assembly: ProvideMetadata(GetType(CustomControlLibrary.VisualStudio.Design.Metadata))> 
    
    Namespace CustomControlLibrary.VisualStudio.Design
    
        ' Container for any general design-time metadata to initialize.
        ' Designers look for a type in the design-time assembly that 
        ' implements IProvideAttributeTable. If found, designers instantiate
        ' this class and access its AttributeTable property automatically.
        Friend Class Metadata
            Implements IProvideAttributeTable
    
            ' Accessed by the designer to register any design-time metadata.
            Public ReadOnly Property AttributeTable() As AttributeTable _
                Implements IProvideAttributeTable.AttributeTable
                Get
                    Dim builder As New AttributeTableBuilder()
    
                    ' Add the adorner provider to the design-time metadata.
                    builder.AddCustomAttributes(GetType(ButtonWithDesignTime), _
                                                New FeatureAttribute(GetType(OpacitySliderAdornerProvider)))
    
                    Return builder.CreateTable()
                End Get
            End Property
    
    
        End Class
    
    End Namespace
    
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.ComponentModel;
    using System.Windows.Media;
    using System.Windows.Controls;
    using System.Windows;
    
    using CustomControlLibrary;
    using Microsoft.Windows.Design.Features;
    using Microsoft.Windows.Design.Metadata;
    
    // The ProvideMetadata assembly-level attribute indicates to designers
    // that this assembly contains a class that provides an attribute table. 
    [assembly: ProvideMetadata(typeof(CustomControlLibrary.VisualStudio.Design.Metadata))]
    namespace CustomControlLibrary.VisualStudio.Design
    {
        // Container for any general design-time metadata to initialize.
        // Designers look for a type in the design-time assembly that 
        // implements IProvideAttributeTable. If found, designers instantiate 
        // this class and access its AttributeTable property automatically.
        internal class Metadata : IProvideAttributeTable
        {
            // Accessed by the designer to register any design-time metadata.
            public AttributeTable AttributeTable
            {
                get 
                {
                    AttributeTableBuilder builder = new AttributeTableBuilder();
    
                    // Add the adorner provider to the design-time metadata.
                    builder.AddCustomAttributes(
                        typeof(ButtonWithDesignTime),
                        new FeatureAttribute(typeof(OpacitySliderAdornerProvider)));
    
                    return builder.CreateTable();
                }
            }
        }
    }
    
  8. Speichern Sie die Projektmappe.

Implementieren des Adorneranbieters

Der Adorneranbieter wird in einem Typ mit dem Namen OpacitySliderAdornerProvider implementiert. Dieser Adorner ermöglicht es dem Benutzer, die Opacity-Eigenschaft des Steuerelements zur Entwurfszeit festzulegen.

So implementieren Sie den Adorneranbieter

  1. Fügen Sie dem CustomControlLibrary.VisualStudio.Design-Projekt eine neue Klasse mit dem Namen OpacitySliderAdornerProvider hinzu.

  2. Ersetzen Sie im Code-Editor für OpacitySliderAdornerProvider den automatisch generierten Code durch den folgenden Code. Mit diesem Code wird ein PrimarySelectionAdornerProvider implementiert, der einen auf einem Slider-Steuerelement basierenden Adorner bereitstellt.

    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;
            }
        }
    }
    
  3. Erstellen Sie die Projektmappe.

Testen der Entwurfszeitimplementierung

Sie können das ButtonWithDesignTime-Steuerelement auf dieselbe Art wie jedes andere WPF-Steuerelement verwenden. Der WPF-Designer verwaltet die Erstellung aller Entwurfszeitobjekte.

So testen Sie die Entwurfszeitimplementierung

  1. Fügen Sie der Projektmappe ein neues WPF-Anwendungsprojekt in Visual Basic oder Visual C# mit dem Namen DemoApplication hinzu.

    Die Datei MainWindow.xaml wird im WPF-Designer geöffnet.

  2. Fügen Sie einen Verweis auf das CustomControlLibrary-Projekt hinzu.

  3. Ersetzen Sie in der XAML-Ansicht den automatisch generierten XAML-Code durch den folgenden XAML-Code. Mit diesem XAML werden ein Verweis auf den CustomControlLibrary-Namespace sowie das benutzerdefinierte ButtonWithDesignTime-Steuerelement hinzugefügt. Die Schaltfläche wird in der Entwurfsansicht mit dem Text "Design mode active" angezeigt, was darauf hindeutet, dass sie sich im Entwurfsmodus befindet. Wenn die Schaltfläche nicht angezeigt wird, müssen Sie möglicherweise auf die Informationsleiste am oberen Rand des Designers klicken, um die Ansicht zu aktualisieren.

    <Window x:Class="DemoApplication.MainWindow"
        xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:cc="clr-namespace:CustomControlLibrary;assembly=CustomControlLibrary"
        Title="Window1" Height="300" Width="300">
        <Grid>
            <cc:ButtonWithDesignTime Margin="30,30,30,30" Background="#FFD4D0C8"></cc:ButtonWithDesignTime>
        </Grid>
    </Window>
    
  4. Klicken Sie in der Entwurfsansicht auf das ButtonWithDesignTime-Steuerelement, um es auszuwählen.

    Ein Slider-Steuerelement wird über dem ButtonWithDesignTime-Steuerelement angezeigt.

  5. Verwenden Sie den Schieberegler-Adorner, um die Durchlässigkeit der Schaltfläche zu ändern.

    In der XAML-Ansicht wird die Opacity-Eigenschaft auf den von dem Slider-Steuerelement angegebenen Wert festgelegt.

  6. Führen Sie das DemoApplication-Projekt aus.

    Zur Laufzeit hat die Schaltfläche die Durchlässigkeit, die Sie mit dem Adorner festlegen.

Nächste Schritte

Sie können benutzerdefinierten Steuerelementen weitere benutzerdefinierte Entwurfszeitfeatures hinzufügen.

Siehe auch

Referenz

PrimarySelectionAdornerProvider

Weitere Ressourcen

Beispiele für WPF-Designer-Erweiterbarkeit

Fortgeschrittene Erweiterungskonzepte

WPF-Designer-Erweiterbarkeit

Bereitstellen von Entwurfszeitmetadaten