Skip to main content

FocusBehavior Class

Focuses a UI control without code-behind.

Namespace: DevExpress.Mvvm.UI

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

NuGet Package: DevExpress.Wpf.Core

Declaration

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

Remarks

Focus a Control Once it is Loaded

In the following code sample, the FocusBehavior focuses a TextBox control when the control is loaded:

<UserControl ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
    <Grid>
        <TextBox>
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:FocusBehavior/>
            </dxmvvm:Interaction.Behaviors>
        </TextBox>
    </Grid>
</UserControl>

Focus a Control When an Event is Raised

  1. Set the SourceObject property (or SourceName) to specify an object that raises the event you want to track.
  2. Specify an object’s event in the EventName property.

The following code sample focuses the TextBox control when a user clicks a Button:

<UserControl ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
    <StackPanel>
        <TextBox Text="This control is focused on button click ">
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:FocusBehavior SourceName="btn" EventName="Click"/>
            </dxmvvm:Interaction.Behaviors>
        </TextBox>
        <Button x:Name="btn" Content="Click to focus the TextBox"/>
    </StackPanel>
</UserControl>

The EventName property accepts only RoutedEvents of UIElements.

Focus a Control When a Property Value is Changed

  1. Set the SourceObject property (or SourceName) to specify an object.
  2. Set PropertyName property to the object’s property you want to track.

The following code sample focuses the TextBox control when the ViewModel’s IsDataLoaded property value is changed:

<UserControl ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm" 
    xmlns:ViewModels="clr-namespace:DXApplication1.ViewModels">
    <UserControl.DataContext>
        <ViewModels:MainViewModel/>
    </UserControl.DataContext>
    <Grid>
        <TextBox Text="This control is focused when data is loaded">
            <dxmvvm:Interaction.Behaviors>
                <dxmvvm:FocusBehavior SourceObject="{Binding ViewModel}" PropertyName="IsDataLoaded"/>
            </dxmvvm:Interaction.Behaviors>
        </TextBox>
    </Grid>
</UserControl>

Add a Focus Delay

Regardless of what triggers the FocusBehavior, you can specify a FocusDelay:

<UserControl ...
    xmlns:dxmvvm="http://schemas.devexpress.com/winfx/2008/xaml/mvvm">
    <StackPanel>
        <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>
        <Button x:Name="btn" Content="Click to focus the TextBox"/>
    </StackPanel>
</UserControl>

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the FocusBehavior class.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

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