Radical Documentation
View on GitHub
Primary version
Primary version
  • Home
  • Presentation
    • AbstractViewModel
    • Conventions
      • Bootstrap Conventions
      • Runtime Conventions
      • Conventions override
    • Commands and DelegateCommand
    • IViewResolver
      • Default view behaviors
      • view life cycle events
        • Callback expectations
        • notify messages
    • Message broker MVVM built-in messages
    • Application boot process
      • Application configuration
      • Application shutdown
      • Singleton applications
    • AbstractMementoViewModel
      • Simple ViewModel graphs
      • Collections and complex ViewModel graphs
    • Validation and Validation Services
    • Resources
      • Services as resources
      • ViewModels as resources
  • UI Composition
    • UI Composition
      • Region content lifecycle
      • TabControl region
      • Create a custom region
  • Concepts
    • Inversion of Control
      • Third party DI containers
    • Entities
      • Property System
    • Messaging and Message Broker
      • POCO messages
      • Standalone message handlers
    • Observers
      • PropertyObserver
      • MementoObserver
      • BrokerObserver
  • Memento
    • Change Tracking Service
      • MementoEntity and MementoEntityCollection
      • Handling change tracking:
        • Simple model
        • Collections
        • Complex objects graph
      • Atomic operations
      • Change Tracking Service API
      • Property Metadata for the ChangeTrackingService
      • Handling collection sync
      • Property State
  • Behaviors
    • DataGrid Behaviors
    • Password
    • Generic routed event handler to command behavior
    • Overlay adorner
      • Busy status manager
    • TextBox behaviors:
      • Command
      • Auto select
      • DisableUndoManager (.Net 3.5 only)
  • Markup Extensions
    • Editor binding
    • Auto Command binding
  • How to
    • Get the view of a given view model
    • Bi-directional communication between different windows/views
    • Handle the busy status during async/long running operations
    • Implement a customer improvement program
    • Manage focus
    • Create a splash screen
    • Access view model after view is closed
    • Intercept ViewModels before they are used
  • Upgrade guides
    • Radical Presentation 1.x to Radical 2.x for .NET Core
    • Radical 2.0.0 to Radical 2.1.0
Powered by GitBook
On this page
  • Manage focus
  • The View
  • The ViewModel
  1. How to

Manage focus

PreviousImplement a customer improvement programNextCreate a splash screen

Last updated 3 years ago

Manage focus

In the Model View ViewModel world there are a lot of things that can be considered borderline, focus management is one of those things. On the other hand managing focus in a desktop application based on WPF is other then a trivial task, focus in desktop application has many facets and lots of corner cases that must be taken into account, there is logical focus, keyboard focus and input scopes that determine the focus behavior.

supports a basic focus management, where input scopes are not supported, and logical focus and keyboard focus are supported assuming they are always related to the same control at the same time.

The View

In order to manage focus on the view side we need to introduce on each control we want to participate the following behavior:

<TextBox Margin="10" Text="{markup:EditorBinding Path=SampleText}">
    <i:Interaction.Behaviors>
        <lb:Focus ControlledBy="{Binding Path=FocusedElementKey}" UsingKey="SampleText" />
    </i:Interaction.Behaviors>
</TextBox>

where the "lb" xml namespace is defined in the http://schemas.radicalframework.com/windows/behaviors XML namespace:

The Focus behavior defines which property of the ViewModel controls the focused element (ControlledBy) and which is the key (UsingKey) that uniquely identifies the control among all the others, in this case we are using, as key, exactly the same name name as the property the control is bound to: "SampleText".

The ViewModel

On the ViewModel side, if we inherit from the base , we just need to decide which should be the “focused” key:

class MainViewModel : AbstractViewModel
{
    public void SetFocus() 
    {
        this.MoveFocusTo(() => this.SampleText);
    }

    public String SampleText
    {
        get { return this.GetPropertyValue(() => this.SampleText); }
        set { this.SetPropertyValue(() => this.SampleText, value); }
    }
}

At runtime each time we call MoveFocusTo, exposed by the base view model we inherit from, the focus is moved to the UI element identified by the given key.

Radical
AbstractViewModel