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
  1. Markup Extensions

Auto Command binding

PreviousEditor bindingNextGet the view of a given view model

Last updated 3 years ago

WPF ICommand interface is the canonical way to expose commands from a ViewModel that is bound to a View. They come with a few caveats:

  • Most of the times commands are simple and the plumbing required to create an ICommand implementation is not worth it.

  • ViewModels exposing commands that implement the ICommand interface reference WPF types with the risk of complicating the required testing infrastructure.

Radical solves the above issues by introducing a handy markup extension that allow to write a ViewModel like the following:

class MyViewModel : AbstractViewModel
{
   public void DoSomething()
   {
      //perform work
   }
}

The DoSomething method can be bound to a Button on the View in the following manner:

<Button Command="{markup:AutoCommandBinding Path=DoSomething}" />

The AutoCommandBinding markup extension will dynamically build a that wraps at runtime the method invocation.

The ICommand interface exposes a CanExecute(object) method that the WPF inteface can call to detect if the command is available in the current context and thus decide if the WPF element bound to a command should be enabled or not. Using the same approach as above a ViewModel can expose a bool property as following:

class MyViewModel : AbstractViewModel
{
   public void DoSomething()
   {
      //perform work
   }
   
   public bool CanDoSomething
   {
      get{ return true; /* or false */ }
   }
}

The convention is to expose a public boolean property whose name is the same as the method, that will be wraped in a command, prefixed with Can. No changes to the XAML markup are required.

class MyViewModel : AbstractViewModel
{
   public MyViewModel()
   {
        this.GetPropertyMetadata( () => this.SelectedEntity )
            .AddCascadeChangeNotifications( () => this.CanEdit );
   }
   
   public MyEntity SelectedEntity
   {
      get { return this.GetPropertyValue( () => this.SelectedEntity ); }
      set { this.SetPropertyValue( () => this.SelectedEntity, value ); }
   }

   public void Edit()
   {
      //perform work
   }
   
   public bool CanEdit
   {
      get{ return this.SelectedEntity != null; }
   }
}

In the above scenario whenever the SelectedEntity property changes a INotifyPropertyChanged.PropertyChanged event is raised also for the CanEdit property, thus WPF reevaluates the property and based on the boolean result the bound command will be enabled or not.

The markup extension is defined in the http://schemas.radicalframework.com/windows/markup xml namespace.

Given the way commands work in WPF one thing that might be required is to change the command status, and thus the bound control, from the ViewModel implementation. The easiest thing is to ask WPF to reevluate the Can* boolean property whenever we decide the command status changes. We can leverage the power of metadata, and specifically the cascade changes notification feature as following:

DelegateCommand
Radical properties