FocusBehavior Class

Allows you to focus a UI control without code-behind usage.

Namespace: DevExpress.Mvvm.UI

Assembly: DevExpress.Xpf.Core.v21.1.dll

Declaration

[TargetType(typeof(Control))]
public class FocusBehavior :
    EventTriggerBase<Control>

Remarks

Focus On Startup

To set the focus when an associated control is loaded, define the FocusBehavior for the control as follows:

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

Focus On an Event or Property Change

To focus a control when an event occurs, specify the EventName property:

<TextBox Text="This control is focused on button click: ">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:FocusBehavior SourceName="button" EventName="Click"/>
    </dxmvvm:Interaction.Behaviors>
</TextBox>
<Button x:Name="button" Content="Click to focus the TextBox"/>

To make a UI control focused when a specific property is changed, specify the FocusBehavior.PropertyName property.

<TextBox Text="This control is focused when data is loaded">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:FocusBehavior SourceObject="{Binding ViewModel}" PropertyName="IsDataLoaded"/>
    </dxmvvm:Interaction.Behaviors>
</TextBox>

The SourceObject and SourceName properties specify an object used for processing an event or property change. The SourceObject can be set through binding. The SourceName is the name of a UI element.

Focus With a Delay

If the associated control should not be focused immediately once it is loaded or the event specified in the EventName property occurs, specify the FocusBehavior.FocusDelay property:

<TextBox Text="This control is focused with a delay">
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:FocusBehavior SourceName="btn"  EventName="Click" FocusDelay="0:00:01"/>
    </dxmvvm:Interaction.Behaviors>
</TextBox>

Example

<UserControl x:Class="Example.View.MainView"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ViewModel="clr-namespace:Example.ViewModel"
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    mc:Ignorable="d" d:DesignHeight="500" d:DesignWidth="600"
    DataContext="{dxmvvm:ViewModelSource Type=ViewModel:MainViewModel}">

    <Grid x:Name="LayoutRoot" Background="White">
        <StackPanel Orientation="Vertical">
            <StackPanel Orientation="Horizontal" Margin="10">
                <TextBlock Text="This control is focused on button click: " VerticalAlignment="Center"/>
                <TextBox Text="Click the bottom button">
                    <dxmvvm:Interaction.Behaviors>
                        <dxmvvm:FocusBehavior SourceName="bt" EventName="Click"/>
                    </dxmvvm:Interaction.Behaviors>
                </TextBox>
                <Button x:Name="bt" Content="Click to focus the TextBox"/>
            </StackPanel>
            <StackPanel Orientation="Horizontal" Margin="10">
                <TextBlock Text="This control is focused on startup: " VerticalAlignment="Center"/>
                <TextBox Text="Focus is here">
                    <dxmvvm:Interaction.Behaviors>
                        <dxmvvm:FocusBehavior/>
                    </dxmvvm:Interaction.Behaviors>
                </TextBox>
            </StackPanel>
        </StackPanel>
    </Grid>
</UserControl> 

Inheritance

Show 11 items
Object
DispatcherObject
DependencyObject
Freezable
Animatable
DevExpress.Mvvm.UI.Interactivity.AttachableObjectBase
DevExpress.Mvvm.UI.Interactivity.Behavior
DevExpress.Mvvm.UI.Interactivity.TriggerBase
DevExpress.Mvvm.UI.Interactivity.TriggerBase<Control>
See Also