Share via


How to: Change the Behavior of a Property at Design Time

When you use extensibility to customize the WPF Designer for Visual Studio, you often create custom controls. Sometimes you need a property of the control to behave differently at design time than at run time, while still letting the user set the value of the property normally. For example, you want the user to be able to set the visible property of a control to false, but the control should still be visible at design time.

This topic describes the procedures to change the design-time behavior of properties of custom controls. For a more detailed example that walks you through step-by-step through the procedures, see Walkthrough: Changing the Behavior of a Property at Design Time.

Important

When you use this technique, the behavior of a property in the designer does not match the value of the property in XAML view. XAML view displays the value that the user entered at design time. The value in XAML view represents the behavior that the property will exhibit at run time.

If you just want to determine whether a control is in design time and set its properties accordingly, see How to: Determine if a Custom Control is in Design Time or Run Time.

Creating a Custom DesignModeValueProvider

In this procedure you create a custom DesignModeValueProvider class. In the TranslatePropertyValue method, you add custom logic for the new behavior of the properties that you want to change. These changes affect the designer only. At run time, the properties behave as set by the user.

To create a custom DesignModeValueProvider

  1. In your project, add a reference to the following assembly:

    • Microsoft.Windows.Design.Extensibility
  2. Add a new class to your custom control project and edit the class to inherit from the DesignModeValueProvider. It should look like the following:

    
    Public Class YourCustomDesignModeValueProvider
        Inherits Microsoft.Windows.Design.Model.DesignModeValueProvider
    
    End Class
    
    
    class YourCustomDesignModeValueProvider : Microsoft.Windows.Design.Model.DesignModeValueProvider
    {
    }
    
  3. Add a constructor to the class. In the constructor you identify the properties that you want to capture.

    
    Public Sub New()
    
        'Properties.Add(<The Property To Change Goes Here>)
        'More properties can go here.
    
    End Sub
    
    
    public YourCustomDesignModeValueProvider()
    {
        //Properties.Add(<The Property To Change Goes Here>);
        //More properties can go here.
    }
    
  4. Override the TranslatePropertyValue method in the class. This is where you specify the new behavior of the properties at design time.

    
    Public Overrides Function TranslatePropertyValue( _
        ByVal item As ModelItem, _
        ByVal identifier As PropertyIdentifier, _
        ByVal value As Object) As Object
    
        'If identifier.Name = <Name of the property to change> Then
        '
        '    Custom logic goes here.
        'End If
    
        'More custom logic for more properties can go here.
    
        Return MyBase.TranslatePropertyValue(item, identifier, value)
    End Function
    
    
    public override object TranslatePropertyValue(ModelItem item, PropertyIdentifier identifier, object value)
    {
        //if (identifier.Name == <Name of the property to change>)
        //{
        //    Custom logic goes here.
        //}
    
        //More custom logic for more properties can go here.
    
        return base.TranslatePropertyValue(item, identifier, value);
    }
    

    Note

    In this procedure, you create one DesignModeValueProvider that handles two different properties. You can also create multiple DesignModeValueProvider objects to handle different properties.

Attaching the DesignModeValueProvider to the Custom Control

In this procedure you attach the DesignModeValueProvider to your custom control by using the FeatureAttribute attribute.

To attach the DesignModeValueProvider to the custom control

  1. Locate the class declaration for your custom control. It should look like the following:

    
    Public Class YourCustomControl
    
    
    class YourCustomControl
    
  2. Add a FeatureAttribute attribute to the class declaration, and specify the DesignModeValueProvider that you created in the previous procedure.

    <Microsoft.Windows.Design.Features.Feature(GetType(YourCustomDesignModeValueProvider))> _
    Public Class YourCustomControl
    
    [Microsoft.Windows.Design.Features.Feature(typeof(YourCustomDesignModeValueProvider))]
    class YourCustomControl
    

    Note

    You can also attach a DesignModeValueProvider to a custom control by providing an attribute table. For more information, see Providing Design-time Metadata.

See Also

Other Resources

Design Time versus Run Time Behavior

Understanding WPF Designer Extensibility

WPF Designer Extensibility