Share via


How to: Determine if a Custom Control is in Design Time or Run Time

When you use extensibility to customize the WPF Designer for Visual Studio, you might want to customize the behavior of a control at design time or run time. You can customize the design-time experience for your end users. For example, if you create a custom button, you can choose to display text on the button at design time, but not at run time. You can also customize a control that exhibits complex behavior so that the behavior only occurs at run time. For example, a control that accesses a database automatically should access the database at run time, but not at design time.

You can determine whether a control is running at design time or at run time by calling the GetIsInDesignMode method.

Using the Design Mode Property

In this procedure you display text on a custom button control at design time, but not at run time.

To use the design mode property

  1. In the code-behind file for your custom control, locate the constructor for your custom control.

  2. Add code that calls the GetIsInDesignMode method, and customize the behavior of your control accordingly. You pass a reference to your custom control as an argument to GetIsInDesignMode and it determines whether the control is in design mode. For example, add code such as the following:

    Namespace CustomControlLibrary
    
        Public Class ButtonWithDesignTime
            Inherits System.Windows.Controls.Button
    
            Public Sub New()
    
                If System.ComponentModel.DesignerProperties.GetIsInDesignMode(Me) Then
                    Me.Content = "This button is in design mode."
                End If
            End Sub
        End Class
    End Namespace
    
    namespace CustomControlLibrary
    {
        public class ButtonWithDesignTime : System.Windows.Controls.Button
        {
            public ButtonWithDesignTime()
            {
                if (System.ComponentModel.DesignerProperties.GetIsInDesignMode(this))
                {
                    this.Content = "This button is in design mode.";
                }
            }
        }
    }
    

See Also

Other Resources

Design Time versus Run Time Behavior

Understanding WPF Designer Extensibility

WPF Designer Extensibility