Skip to main content
A newer version of this page is available. .

Getting Started

  • 2 minutes to read

The Behavior mechanism extends the capabilities of visual controls and adheres to the MVVM pattern. You can implement missing functionality with a custom Behavior and begin using it any place in your application without code-behind and duplicated code.

Regular approach to define Behaviors

The DevExpress.Mvvm.UI.Interactivity.Interaction class implements a Behaviors attached property. This property is read-only and contains a collection of Behaviors. To use a Behavior, place it in this collection in XAML.

<TextBox Text="This control is focused on startup">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:FocusBehavior/>
        <!--Other Behaviors-->
    </dxmvvm:Interaction.Behaviors>
</TextBox>

Behaviors can provide dependency properties, which you can set or bind. For instance:

<ListBox ...> 
    <dxmvvm:Interaction.Behaviors> 
        <dxmvvm:EventToCommand EventName="MouseDoubleClick" Command="{Binding EditCommand}"> 
            <dxmvvm:EventToCommand.EventArgsConverter> 
                <Common:ListBoxEventArgsConverter/> 
            </dxmvvm:EventToCommand.EventArgsConverter> 
        </dxmvvm:EventToCommand> 
    </dxmvvm:Interaction.Behaviors> 
    ...
</ListBox>

How to define and use Behaviors in styles

The DevExpress.Mvvm.UI.Interactivity.Interaction class implements a BehaviorsTemplate attached property. This property can be used to apply Behaviors in a style. For instance:

<Style TargetType="TextBox">
    <Setter Property="dxmvvm:Interaction.BehaviorsTemplate">
        <Setter.Value>
            <DataTemplate>
                <ContentControl>
                    <dxmvvm:FocusBehavior/>
                </ContentControl>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>

If it is necessary to define more than one Behavior, you can place behaviors on the ItemsControl element, instead of the ContentControl.

<Style TargetType="TextBox">
    <Setter Property="dxmvvm:Interaction.BehaviorsTemplate">
        <Setter.Value>
            <DataTemplate>
                <ItemsControl>
                    <dxmvvm:FocusBehavior/>
                    <dxmvvm:EventToCommand EventName="MouseDoubleClick" Command="{Binding EditCommand}"> 
                        <dxmvvm:EventToCommand.EventArgsConverter> 
                            <Common:ListBoxEventArgsConverter/> 
                        </dxmvvm:EventToCommand.EventArgsConverter> 
                    </dxmvvm:EventToCommand> 
                </ItemsControl>
            </DataTemplate>
        </Setter.Value>
    </Setter>
</Style>