New to Telerik UI for WPF? Download free 30-day trial

AutoBind Attached Behavior

The need of reusable DataTemplates is a common scenario with quite few universal viable solutions. The AutoBind attached behavior enables RadPropertyGrid to use a single DataTemplate resource as an EditorTemplate value for multiple PropertyDefinitions without any additional effort on the users’ side.

Here is the API reference for the AutoBindBehaviour.

Here is an example with RadPropertyGrid that has its Item property bound to a Button:

Example 1: Using AutoBindBehavior

<Grid x:Name="LayoutRoot" Background="White"> 
  <Grid.Resources> 
    <DataTemplate x:Key="editorTemplate"> 
      <TextBox Foreground="Red" FontWeight="Bold" telerik:AutoBindBehavior.UpdateBindingOnElementLoaded="Text" /> 
    </DataTemplate> 
  </Grid.Resources> 
  <telerik:RadPropertyGrid x:Name="rpg" AutoGeneratePropertyDefinitions="False"> 
    <telerik:RadPropertyGrid.PropertyDefinitions> 
      <telerik:PropertyDefinition Binding="{Binding Height}" 
                                  EditorTemplate="{StaticResource editorTemplate}" 
                                  DisplayName="Height" /> 
      <telerik:PropertyDefinition Binding="{Binding Width}" 
                                  EditorTemplate="{StaticResource editorTemplate}" 
                                  DisplayName="Width" /> 
      <telerik:PropertyDefinition Binding="{Binding ActualHeight}" 
                                  IsReadOnly="True" 
                                  DisplayName="ActualHeight"/> 
      <telerik:PropertyDefinition Binding="{Binding ActualWidth}" 
                                  IsReadOnly="True" 
                                  DisplayName="ActualWidth"/> 
    </telerik:RadPropertyGrid.PropertyDefinitions> 
  </telerik:RadPropertyGrid> 
</Grid> 

Rad Property Grid Sets Autobind

The AutoBind attached behavior is also available in scenarios with auto-generated fields. In order to achieve this one can either set a PropertyDefinition’s EditorTemplate on the AutogeneratingDataField, or utilize a DataTemplateSelector.

TwoWay binding with the AutoBindBehavior is supported for the following types:

  • Primitive types

  • String

  • Decimal

  • Guid

  • DateTime

  • Enum

  • Color

  • TimeSpan

  • GridLength

  • Thickness

How does it work?

In most cases when a custom DataTemplate is defined, the element within it would have to be explicitly bound to a particular property. This requires a DataTemplate to be defined for each property as it cannot be reused. The AutoBindBehavior provides a way to define a more generic DataTemplate and reuse it for editing different properties. Shortly said, it matches the property of the editor with the one bound to a given PropertyDefinition. This is done through the UpdateBindingOnElementLoaded property of the behavior. It has to be set to point to the property of the given editor that is used for editing a property value. Apparently, in our example it is TextBox’s Text DependencyProperty.

The AutoBind behavior is designed to function exclusively in the context of RadPropertyGrid.

Set the BindingMode of Custom Data Type

By default, the AutoBindBehavior supports TwoWay Binding only for the data types listed in the previous section. For a custom data type, the BindingMode is OneWay. This behavior can be altered through the BindingModeOverride property.

Example 2: Setting the BindingModeOverride property to TwoWay

<Grid.Resources> 
  <DataTemplate x:Key="editorTemplate1"> 
    <TextBox Foreground="Red" FontWeight="Bold" 
             telerik:AutoBindBehavior.BindingModeOverride="TwoWay"/> 
  </DataTemplate> 
</Grid.Resources> 

See Also

In this article