# Behaviors | WPF Controls | DevExpress Documentation

Behaviors can attach to visual controls and customize them (or extend their capabilities). Use this customization technique if you want your application to follow the MVVM pattern (no code-behind in Views). Our WPF product line ships with a wide variety of pre-defined Behaviors. You can find the list later in this topic. If no-built in Behavior meets your requirements, you can implement a custom class as described in the last section.

[Run Demo: Behaviors Module in the WPF MVVM Demo](dxdemo://Wpf/DXMVVM/MainDemo/Behaviors)

## Add Behaviors to a Control

### In XAML

The [Interactivity.Interaction](/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction) class implements the [Behaviors](/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.Behaviors) attached property. To use a Behavior, place it in the following collection:

- MainView.xaml

<section id="tabpanel_bZueoalD1J_tabid-xaml" role="tabpanel" data-tab="tabid-xaml">
<pre><code data-code-links="{&quot;BarSubItem&quot;:&quot;/WPF/DevExpress.Xpf.Bars.BarSubItem&quot;,&quot;GetBehaviors&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.GetBehaviors(System.Windows.DependencyObject)&quot;,&quot;KeyToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.KeyToCommand&quot;,&quot;EventToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.EventToCommand&quot;}" class="lang-xaml">&lt;TextBox Text=&quot;This control is focused on startup&quot;&gt;
    &lt;dxmvvm:Interaction.Behaviors&gt;
        &lt;dxmvvm:FocusBehavior/&gt;
        &lt;!--Other Behaviors--&gt;
    &lt;/dxmvvm:Interaction.Behaviors&gt;
&lt;/TextBox&gt;
</code></pre></section>

Behaviors may contain dependency properties:

- MainView.xaml

<section id="tabpanel_bZueoalD1J-1_tabid-xaml" role="tabpanel" data-tab="tabid-xaml">
<pre><code data-code-links="{&quot;BarSubItem&quot;:&quot;/WPF/DevExpress.Xpf.Bars.BarSubItem&quot;,&quot;GetBehaviors&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.GetBehaviors(System.Windows.DependencyObject)&quot;,&quot;KeyToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.KeyToCommand&quot;,&quot;EventToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.EventToCommand&quot;}" data-highlight-lines="[[3]]" class="lang-xaml">&lt;ListBox ...&gt; 
    &lt;dxmvvm:Interaction.Behaviors&gt; 
        &lt;dxmvvm:EventToCommand EventName=&quot;MouseDoubleClick&quot; Command=&quot;{Binding EditCommand}&quot;&gt; 
            &lt;dxmvvm:EventToCommand.EventArgsConverter&gt; 
                &lt;Common:ListBoxEventArgsConverter/&gt; 
            &lt;/dxmvvm:EventToCommand.EventArgsConverter&gt; 
        &lt;/dxmvvm:EventToCommand&gt; 
    &lt;/dxmvvm:Interaction.Behaviors&gt; 
    ...
&lt;/ListBox&gt;
</code></pre></section>

### In Code-Behind

The following code sample attaches a [BarSubItemThemeSelectorBehavior](/WPF/DevExpress.Xpf.Bars.BarSubItemThemeSelectorBehavior) to a BarSubItem in code-behind. This example hides touch themes from the theme selector (sets the **ShowTouchThemes** property to `false`):

- MainWindow.xaml

<section id="tabpanel_bZueoalD1J-2_tabid-xaml" role="tabpanel" data-tab="tabid-xaml">
<pre><code data-code-links="{&quot;BarSubItem&quot;:&quot;/WPF/DevExpress.Xpf.Bars.BarSubItem&quot;,&quot;GetBehaviors&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.GetBehaviors(System.Windows.DependencyObject)&quot;,&quot;KeyToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.KeyToCommand&quot;,&quot;EventToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.EventToCommand&quot;}" data-highlight-lines="[[9]]" class="lang-xaml">&lt;Window ...
    xmlns:dxb=&quot;http://schemas.devexpress.com/winfx/2008/xaml/bars&quot;
    xmlns:dxr=&quot;http://schemas.devexpress.com/winfx/2008/xaml/ribbon&quot;&gt;
    &lt;StackPanel&gt;
        &lt;dxr:RibbonControl&gt;
            &lt;dxr:RibbonDefaultPageCategory&gt;
                &lt;dxr:RibbonPage Caption=&quot;Home&quot;&gt;
                    &lt;dxr:RibbonPageGroup Caption=&quot;File&quot;&gt;
                        &lt;dxb:BarSubItem x:Name=&quot;barsubitem1&quot;/&gt;
                    &lt;/dxr:RibbonPageGroup&gt;
                &lt;/dxr:RibbonPage&gt;
            &lt;/dxr:RibbonDefaultPageCategory&gt;
        &lt;/dxr:RibbonControl&gt;
    &lt;/StackPanel&gt;
&lt;/Window&gt;
</code></pre></section>

- MainWindow.xaml.cs
- MainWindow.xaml.vb

<section id="tabpanel_bZueoalD1J-3_tabid-csharp" role="tabpanel" data-tab="tabid-csharp">
<pre><code data-code-links="{&quot;BarSubItem&quot;:&quot;/WPF/DevExpress.Xpf.Bars.BarSubItem&quot;,&quot;GetBehaviors&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.GetBehaviors(System.Windows.DependencyObject)&quot;,&quot;KeyToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.KeyToCommand&quot;,&quot;EventToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.EventToCommand&quot;,&quot;/ (DevExpress.Mvvm.UI.Interactivity)(?:;|$)/&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity&quot;,&quot;/ (DevExpress.Xpf.Bars)(?:;|$)/&quot;:&quot;/WPF/DevExpress.Xpf.Bars&quot;}" data-highlight-lines="[[8]]" class="lang-csharp">using DevExpress.Mvvm.UI.Interactivity;
using DevExpress.Xpf.Bars;

// ...

    public MainWindow() {
        InitializeComponent();
        Interaction.GetBehaviors(barsubitem1).Add(new BarSubItemThemeSelectorBehavior {ShowTouchThemes=false });
    }
</code></pre></section>
<section id="tabpanel_bZueoalD1J-3_tabid-vb" role="tabpanel" data-tab="tabid-vb" aria-hidden="true" hidden="hidden">
<pre><code data-code-links="{&quot;BarSubItem&quot;:&quot;/WPF/DevExpress.Xpf.Bars.BarSubItem&quot;,&quot;GetBehaviors&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.GetBehaviors(System.Windows.DependencyObject)&quot;,&quot;KeyToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.KeyToCommand&quot;,&quot;EventToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.EventToCommand&quot;,&quot;/ (DevExpress.Mvvm.UI.Interactivity)(?:;|$)/&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity&quot;,&quot;/ (DevExpress.Xpf.Bars)(?:;|$)/&quot;:&quot;/WPF/DevExpress.Xpf.Bars&quot;}" class="lang-vb">Imports DevExpress.Mvvm.UI.Interactivity
Imports DevExpress.Xpf.Bars

Public Sub New()
    InitializeComponent()
    Interaction.GetBehaviors(barsubitem1).Add(New BarSubItemThemeSelectorBehavior With {
        .ShowTouchThemes = False
    })
End Sub
</code></pre></section>

## Add Behaviors to Controls of the Specified Type

### Add a Single Behavior

You can attach a behavior to all objects of a specific type (and descendants) on a Window or a UserControl. To do that, place your declaration in [Style](https://learn.microsoft.com/dotnet/api/system.windows.style). The following code sample uses the [BehaviorsTemplate](/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.BehaviorsTemplate) attached property to add the [KeyToCommand](/WPF/DevExpress.Mvvm.UI.KeyToCommand) behavior to each element of the [GridControl](/WPF/6084/controls-and-libraries/data-grid) type:

- XAML

<section id="tabpanel_bZueoalD1J-4_tabid-xaml" role="tabpanel" data-tab="tabid-xaml">
<pre><code data-code-links="{&quot;BarSubItem&quot;:&quot;/WPF/DevExpress.Xpf.Bars.BarSubItem&quot;,&quot;GetBehaviors&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.GetBehaviors(System.Windows.DependencyObject)&quot;,&quot;KeyToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.KeyToCommand&quot;,&quot;EventToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.EventToCommand&quot;}" data-highlight-lines="[[1],[6]]" class="lang-xaml">&lt;Style TargetType=&quot;dxg:GridControl&quot;&gt;
    &lt;Setter Property=&quot;dxmvvm:Interaction.BehaviorsTemplate&quot;&gt;
        &lt;Setter.Value&gt;
            &lt;DataTemplate&gt;
                &lt;ContentControl&gt;
                    &lt;dxmvvm:KeyToCommand KeyGesture=&quot;CTRL+U&quot; Command=&quot;{Binding UpdateCommand}&quot;/&gt;
                &lt;/ContentControl&gt;
            &lt;/DataTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;
</code></pre></section>

### Add Multiple Behaviors

Place multiple behaviors to the **ItemsControl** element. The following code sample adds [KeyToCommand](/WPF/DevExpress.Mvvm.UI.KeyToCommand) and [EventToCommand](/WPF/DevExpress.Mvvm.UI.EventToCommand) behaviors to each element of the [GridControl](/WPF/6084/controls-and-libraries/data-grid) type and its descendants:

- MainView.xaml

<section id="tabpanel_bZueoalD1J-5_tabid-xaml" role="tabpanel" data-tab="tabid-xaml">
<pre><code data-code-links="{&quot;BarSubItem&quot;:&quot;/WPF/DevExpress.Xpf.Bars.BarSubItem&quot;,&quot;GetBehaviors&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.GetBehaviors(System.Windows.DependencyObject)&quot;,&quot;KeyToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.KeyToCommand&quot;,&quot;EventToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.EventToCommand&quot;}" data-highlight-lines="[[1],[4],[6],[7]]" class="lang-xaml">&lt;Style TargetType=&quot;dxg:GridControl&quot;&gt;
    &lt;Setter Property=&quot;dxmvvm:Interaction.BehaviorsTemplate&quot;&gt;
        &lt;Setter.Value&gt;
            &lt;DataTemplate&gt;
                &lt;ItemsControl&gt;
                    &lt;dxmvvm:KeyToCommand KeyGesture=&quot;CTRL+U&quot; Command=&quot;{Binding UpdateCommand}&quot;/&gt;
                    &lt;dxmvvm:EventToCommand EventName=&quot;MouseDoubleClick&quot; Command=&quot;{Binding EditCommand}&quot;&gt; 
                        &lt;dxmvvm:EventToCommand.EventArgsConverter&gt; 
                            &lt;Common:ListBoxEventArgsConverter/&gt; 
                        &lt;/dxmvvm:EventToCommand.EventArgsConverter&gt; 
                    &lt;/dxmvvm:EventToCommand&gt; 
                &lt;/ItemsControl&gt;
            &lt;/DataTemplate&gt;
        &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
&lt;/Style&gt;
</code></pre></section>

## Predefined Behaviors

DevExpress WPF Product Line includes the following predefined behaviors:

- [EventToCommand](/WPF/DevExpress.Mvvm.UI.EventToCommand)

    - Executes a command in response to a raised event.

- [KeyToCommand](/WPF/DevExpress.Mvvm.UI.KeyToCommand)

    - Allows you to bind a [KeyGesture](https://learn.microsoft.com/dotnet/api/system.windows.input.keygesture) to a command.

- [FocusBehavior](/WPF/DevExpress.Mvvm.UI.FocusBehavior)

    - Focuses a UI control without code-behind.

- [ValidationErrorsHostBehavior](/WPF/DevExpress.Mvvm.UI.ValidationErrorsHostBehavior)

    - Tracks validation errors within a UI container.

- [ConfirmationBehavior](/WPF/DevExpress.Mvvm.UI.ConfirmationBehavior)

    - Shows a confirmation message box before an application executes the specified command. Users can confirm or cancel the operation.

- [DependencyPropertyBehavior](/WPF/DevExpress.Mvvm.UI.DependencyPropertyBehavior)

    - Allows you to bind a ViewModel property to a control’s non-dependency property.

- [EnumItemsSourceBehavior](/WPF/DevExpress.Mvvm.UI.EnumItemsSourceBehavior)

    - Binds a control’s **ItemsSource** property to an enumeration.

- [CompositeCommandBehavior](/WPF/DevExpress.Mvvm.UI.CompositeCommandBehavior)

    - Allows you to aggregate multiple commands and execute them in a specific order.

- [FunctionBindingBehavior](/WPF/DevExpress.Mvvm.UI.FunctionBindingBehavior)

    - Binds a ViewModel function’s result to a control’s property.

- [MethodToCommandBehavior](/WPF/DevExpress.Mvvm.UI.MethodToCommandBehavior)

    - Binds a method of a ViewModel or control to a [Command](/WPF/DevExpress.Mvvm.UI.MethodToCommandBehavior.Command) property of a control on a View.

- [ReadOnlyDependencyPropertyBindingBehavior](/WPF/DevExpress.Mvvm.UI.ReadOnlyDependencyPropertyBindingBehavior)

    - Allows you to bind read-only dependency and attached properties to a ViewModel’s properties.

- [CurrentWindowSerializationBehavior](/WPF/DevExpress.Mvvm.UI.CurrentWindowSerializationBehavior)

    - Allows you to serialize/deserialize settings (size and state) of the associated view (or window).

- [BarSubItemThemeSelectorBehavior](/WPF/DevExpress.Xpf.Bars.BarSubItemThemeSelectorBehavior)

    - Populates the associated [BarSubItem](/WPF/DevExpress.Xpf.Bars.BarSubItem) with available themes and allows you to choose the application’s theme.

- [BarSplitItemThemeSelectorBehavior](/WPF/DevExpress.Xpf.Bars.BarSplitItemThemeSelectorBehavior)

    - Populates the associated [BarSplitButtonItem](/WPF/DevExpress.Xpf.Bars.BarSplitButtonItem) with available themes and allows you to choose the application’s theme.

- [RibbonGalleryItemThemeSelectorBehavior](/WPF/DevExpress.Xpf.Ribbon.RibbonGalleryItemThemeSelectorBehavior)

    - Populates the associated [RibbonGalleryBarItem](/WPF/DevExpress.Xpf.Ribbon.RibbonGalleryBarItem) with available themes and allows you to choose the application’s theme.

- [GalleryThemeSelectorBehavior](/WPF/DevExpress.Xpf.Bars.GalleryThemeSelectorBehavior)

    - Populates the associated [GalleryControl](/WPF/DevExpress.Xpf.Bars.GalleryControl) with available themes and allows you to choose the application’s theme.

- [HamburgerSubMenuThemeSelectorBehavior](/WPF/DevExpress.Xpf.WindowsUI.HamburgerSubMenuThemeSelectorBehavior)

    - Populates the associated [HamburgerSubMenu](/WPF/DevExpress.Xpf.WindowsUI.HamburgerSubMenu) with available themes and allows you to choose the application’s theme.

- [RibbonGalleryItemThemePaletteSelectorBehavior](/WPF/DevExpress.Xpf.Ribbon.RibbonGalleryItemThemePaletteSelectorBehavior)

    - Populates the associated [RibbonGalleryBarItem](/WPF/DevExpress.Xpf.Ribbon.RibbonGalleryBarItem) with the available palettes and allows you to choose the application theme’s palette.

## Create a Custom Behavior

Each Behavior is a **Behavior&lt;T&gt;** class descendant. The **T** parameter defines the associated control type. The following code sample specifies a Behavior that you can apply only to a [TextBox](https://learn.microsoft.com/dotnet/api/system.windows.controls.textbox) control or its descendants:

- C#
- VB.NET

<section id="tabpanel_bZueoalD1J-6_tabid-csharp" role="tabpanel" data-tab="tabid-csharp">
<pre><code data-code-links="{&quot;BarSubItem&quot;:&quot;/WPF/DevExpress.Xpf.Bars.BarSubItem&quot;,&quot;GetBehaviors&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.GetBehaviors(System.Windows.DependencyObject)&quot;,&quot;KeyToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.KeyToCommand&quot;,&quot;EventToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.EventToCommand&quot;}" class="lang-csharp">public class ValidationBehavior : Behavior&lt;TextBox&gt; // TextBox is the AssociatedObject type 
{
    protected override void OnAttached() {
        base.OnAttached();
        // subscribe to the AssociatedObject events
    }
    protected override void OnDetaching() {
        // unsubscribe from the AssociatedObject events
        base.OnDetaching();
    }
}
</code></pre></section>
<section id="tabpanel_bZueoalD1J-6_tabid-vb" role="tabpanel" data-tab="tabid-vb" aria-hidden="true" hidden="hidden">
<pre><code data-code-links="{&quot;BarSubItem&quot;:&quot;/WPF/DevExpress.Xpf.Bars.BarSubItem&quot;,&quot;GetBehaviors&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.GetBehaviors(System.Windows.DependencyObject)&quot;,&quot;KeyToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.KeyToCommand&quot;,&quot;EventToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.EventToCommand&quot;}" class="lang-vb">Class SurroundingClass
    Public Class ValidationBehavior
    &#39; TextBox is the AssociatedObject type 
        Inherits Behavior(Of TextBox)
    End Class

    Protected Overrides Sub OnAttached()
    &#39; subscribe to the AssociatedObject events
        MyBase.OnAttached()
    End Sub

    Protected Overrides Sub OnDetaching()
    &#39;unsubscribe from the AssociatedObject events
        MyBase.OnDetaching()
    End Sub
End Class
</code></pre></section>

The **Behavior&lt;T&gt;** class contains the **AssociatedObject** property. DevExpress MVVM Framework specifies this property when you add a Behavior to the Behaviors collection. 

After the AssociatedObject is specified, DevExpress MVVM Framework invokes the virtual **OnAttached** method. You can override this method to subscribe to AssociatedObject’s events and initialize its properties. 

To unsubscribe from events, you can use the virtual **OnDetaching** method. DevExpress MVVM Framework invokes when the Behavior is destroyed.

- C#
- VB.NET

<section id="tabpanel_bZueoalD1J-7_tabid-csharp" role="tabpanel" data-tab="tabid-csharp">
<pre><code data-code-links="{&quot;BarSubItem&quot;:&quot;/WPF/DevExpress.Xpf.Bars.BarSubItem&quot;,&quot;GetBehaviors&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.GetBehaviors(System.Windows.DependencyObject)&quot;,&quot;KeyToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.KeyToCommand&quot;,&quot;EventToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.EventToCommand&quot;}" class="lang-csharp">public class ValidationBehavior : Behavior&lt;TextBox&gt; {
    public static readonly DependencyProperty ValidForegroundProperty =
        DependencyProperty.Register(&quot;ValidForeground&quot;, typeof(Brush), typeof(ValidationBehavior),
        new PropertyMetadata(new SolidColorBrush(Colors.Black), (d, e) =&gt; ((ValidationBehavior)d).Update()));
    public static readonly DependencyProperty InvalidForegroundProperty =
        DependencyProperty.Register(&quot;InvalidForeground&quot;, typeof(Brush), typeof(ValidationBehavior), 
        new PropertyMetadata(new SolidColorBrush(Colors.Red), (d,e) =&gt; ((ValidationBehavior)d).Update()));
    public static readonly DependencyProperty InvalidValueProperty =
        DependencyProperty.Register(&quot;InvalidValue&quot;, typeof(string), typeof(ValidationBehavior),
        new PropertyMetadata(string.Empty, (d, e) =&gt; ((ValidationBehavior)d).Update()));
    public Brush ValidForeground {
        get { return (Brush)GetValue(ValidForegroundProperty); }
        set { SetValue(ValidForegroundProperty, value); }
    }
    public Brush InvalidForeground {
        get { return (Brush)GetValue(InvalidForegroundProperty); }
        set { SetValue(InvalidForegroundProperty, value); }
    }
    public string InvalidValue {
        get { return (string)GetValue(InvalidValueProperty); }
        set { SetValue(InvalidValueProperty, value); }
    }

    protected override void OnAttached() {
        base.OnAttached();
        AssociatedObject.TextChanged += OnAssociatedObjectTextChanged;
        Update();
    }
    protected override void OnDetaching() {
        AssociatedObject.TextChanged -= OnAssociatedObjectTextChanged;
        base.OnDetaching();
    }
    void OnAssociatedObjectTextChanged(object sender, TextChangedEventArgs e) {
        Update();
    }
    void Update() {
        if(AssociatedObject == null) return;
        if(AssociatedObject.Text == InvalidValue)
            AssociatedObject.Foreground = InvalidForeground;
        else AssociatedObject.Foreground = ValidForeground;
    }
}
</code></pre></section>
<section id="tabpanel_bZueoalD1J-7_tabid-vb" role="tabpanel" data-tab="tabid-vb" aria-hidden="true" hidden="hidden">
<pre><code data-code-links="{&quot;BarSubItem&quot;:&quot;/WPF/DevExpress.Xpf.Bars.BarSubItem&quot;,&quot;GetBehaviors&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.GetBehaviors(System.Windows.DependencyObject)&quot;,&quot;KeyToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.KeyToCommand&quot;,&quot;EventToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.EventToCommand&quot;}" class="lang-vb">Public Class ValidationBehavior
    Inherits Behavior(Of TextBox)

    Public Shared ReadOnly ValidForegroundProperty As DependencyProperty = DependencyProperty.Register(&quot;ValidForeground&quot;, GetType(Brush), GetType(ValidationBehavior), New PropertyMetadata(New SolidColorBrush(Colors.Black), Sub(d, e) CType(d, ValidationBehavior).Update()))
    Public Shared ReadOnly InvalidForegroundProperty As DependencyProperty = DependencyProperty.Register(&quot;InvalidForeground&quot;, GetType(Brush), GetType(ValidationBehavior), New PropertyMetadata(New SolidColorBrush(Colors.Red), Sub(d,e) CType(d, ValidationBehavior).Update()))
    Public Shared ReadOnly InvalidValueProperty As DependencyProperty = DependencyProperty.Register(&quot;InvalidValue&quot;, GetType(String), GetType(ValidationBehavior), New PropertyMetadata(String.Empty, Sub(d, e) CType(d, ValidationBehavior).Update()))
    Public Property ValidForeground() As Brush
        Get
            Return CType(GetValue(ValidForegroundProperty), Brush)
        End Get
        Set(ByVal value As Brush)
            SetValue(ValidForegroundProperty, value)
        End Set
    End Property
    Public Property InvalidForeground() As Brush
        Get
            Return CType(GetValue(InvalidForegroundProperty), Brush)
        End Get
        Set(ByVal value As Brush)
            SetValue(InvalidForegroundProperty, value)
        End Set
    End Property
    Public Property InvalidValue() As String
        Get
            Return CStr(GetValue(InvalidValueProperty))
        End Get
        Set(ByVal value As String)
            SetValue(InvalidValueProperty, value)
        End Set
    End Property

    Protected Overrides Sub OnAttached()
        MyBase.OnAttached()
        AddHandler AssociatedObject.TextChanged, AddressOf OnAssociatedObjectTextChanged
        Update()
    End Sub
    Protected Overrides Sub OnDetaching()
        RemoveHandler AssociatedObject.TextChanged, AddressOf OnAssociatedObjectTextChanged
        MyBase.OnDetaching()
    End Sub
    Private Sub OnAssociatedObjectTextChanged(ByVal sender As Object, ByVal e As TextChangedEventArgs)
        Update()
    End Sub
    Private Sub Update()
        If AssociatedObject Is Nothing Then
            Return
        End If
        If AssociatedObject.Text = InvalidValue Then
            AssociatedObject.Foreground = InvalidForeground
        Else
            AssociatedObject.Foreground = ValidForeground
        End If
    End Sub
End Class
</code></pre></section>

- MainView.xaml

<section id="tabpanel_bZueoalD1J-8_tabid-xaml" role="tabpanel" data-tab="tabid-xaml">
<pre><code data-code-links="{&quot;BarSubItem&quot;:&quot;/WPF/DevExpress.Xpf.Bars.BarSubItem&quot;,&quot;GetBehaviors&quot;:&quot;/WPF/DevExpress.Mvvm.UI.Interactivity.Interaction.GetBehaviors(System.Windows.DependencyObject)&quot;,&quot;KeyToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.KeyToCommand&quot;,&quot;EventToCommand&quot;:&quot;/WPF/DevExpress.Mvvm.UI.EventToCommand&quot;}" class="lang-xaml">&lt;TextBox Text=&quot;Text&quot;&gt;
    &lt;dxmvvm:Interaction.Behaviors&gt;
        &lt;Behaviors:ValidationBehavior ValidForeground=&quot;{Binding ValidBrush}&quot;
                                      InvalidForeground=&quot;{Binding InvalidBrush}&quot;
                                      InvalidValue=&quot;{Binding InvalidValue}&quot;/&gt;
    &lt;/dxmvvm:Interaction.Behaviors&gt;
&lt;/TextBox&gt;
</code></pre></section>

[View Example: WPF MVVM Behaviors - Create a Custom Attached Behavior](https://github.com/DevExpress-Examples/wpf-mvvm-behaviors-create-a-custom-behavior)