Como: alterar o comportamento de uma propriedade em tempo de design
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: Alterar o comportamento de uma propriedade em tempo de design.
Importante |
---|
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 Como: determinar se um controle personalizado está em tempo de design ou 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
In your project, add a reference to the following assembly:
- Microsoft.Windows.Design.Extensibility
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 { }
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. }
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); }
Observação 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
Locate the class declaration for your custom control. It should look like the following:
Public Class YourCustomControl
class YourCustomControl
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
Observação Você também pode anexar um DesignModeValueProvider a um controle personalizado , fornecendo uma tabela de atributo . For more information, see Fornecimento de metadados de tempo de design.
Consulte também
Outros recursos
Tempo de design versus Run Time comportamento