Getting Started

Step 1: Define the view model

The view model is the contract between the controller and the view. This should have the properties that may be
bound to from the view, including any commands (ICommand).


public interface IDemoViewModel
    string Title { get; set; }
    string Text { get; set; }
    long Counter { get; set; }
    IList<string> List { get; set; }
    ICommand Command { get; set; }

Step 2: Define the controller service

The controller service hosts the command behavior for the view model.


public interface IViewModelController
    void Command(IDemoViewModel demoViewModel);
    void Initialize(IDemoViewModel demoViewModel);

Step 3: Configure the view model

The view model configuration controls how the view model behaves at runtime.

This includes:
  • Binding to a validator
  • Binding to a controller
  • Whether singleton or transient instances are returned from the container
  • Identifying any properties that should have INotifyPropertyChanged behavior
  • Identifying commands that should be bound to the controller
  • Initializing specific properties after construction


public class DemoViewModelConfiguration : ConfigureViewModel<IDemoViewModel>
    public DemoViewModelConfiguration()
        Notify(x => x.Title);
        Notify(x => x.Text);
        Notify(x => x.Counter);
        Command(x => x.Command);
        Initialize(x => x.List, () => new ObservableCollection<string>());

Step 4: Configure the validator

This step is optional. However you can configure the view model to be validated using
FluentValidation ( as the validator.


public class DemoViewModelValidator : ValidationConfiguration<IDemoViewModel>
    public DemoViewModelValidator()
        RuleFor(x => x.Text)

Step 5: Build the controller service

The controller is responsible for making the whole thing run. It hosts the commands that are
used by view models, and optionally an initialization method, one for each view model bound
to the controller, that is called when a new view model is constructed.

For commands to be wired up, the view model must identify a command to be connected,
and the controller must have a method that takes a single paramter for the view model and
must have the same name as the ICommand property on the view model. Any interaction
with the command from the view model will be dispatched to the controller.


public class ViewModelController : IViewModelController
    public void Command(IDemoViewModel demoViewModel)

    public void Initialize(IDemoViewModel demoViewModel)
        demoViewModel.Title = "View Model Demo!";
        demoViewModel.Text = "Welcome, Fluency!";

Step 6: Configure the controller service

The controller configuration controls how the controller behaves at runtime.

This includes:
  • Providing an implementation
  • Whether singleton or transient instances are returned from the container

public class ViewModelControllerConfiguration : ConfigureController<IViewModelController>
    public ViewModelControllerConfiguration()

Step 7: Build the configuration

Building the configuration prepares the environment and wires everything up.


protected override void OnStartup(StartupEventArgs e)

Step 8: Bind to the view model

Binding to a view model kicks the whole thing off, and causes any related controllers and view models to be built and presented.


    <fvm:ViewModelActivator Type="{x:Type demo:IDemoViewModel}"/>
<StackPanel Orientation="Vertical">
        <DataTemplate DataType="{x:Type ValidationError}">
            <TextBlock FontStyle="Italic"
                        Text="{Binding Path=ErrorContent}" />
    <TextBox x:Name="Text"
                Text="{Binding Path=Text, ValidatesOnDataErrors=True, UpdateSourceTrigger=PropertyChanged}"
    <ContentPresenter Content="{Binding ElementName=Text, Path=(Validation.Errors).CurrentItem}" />
    <Button Height="30"
            Command="{Binding Path=Command}" />
    <TextBlock Height="30"
                Text="{Binding Path=Counter}" />

Step 9: Relax

This example is based on the TestApplication that is provided in the source code.

Last edited Apr 28, 2010 at 3:38 AM by bobble79, version 14


No comments yet.