This project is read-only.

Wiring view commands with methods of the model

Here is how the View's DataContext is initialized properly to accept the DynamicViewModel(Of TModel) Class wrapper around the model class:

internal partial class ContactView : UserControl
{
    public static readonly RoutedCommand ClearNamesCommand  = new RoutedCommand();
    public static readonly RoutedCommand NavigateUriCommand = new RoutedCommand();
 
    public ContactView()
    {
        InitializeComponent();
 
        // Create a new instance. Once created
        // do not call methods directly on this
        // object. (Use the dynamic viewModel).
        var instance  = new ContactDetails() {
            FirstName = "Nikos",
            LastName  = "Baxevanis"
        };
 
        dynamic viewModel = new DynamicViewModel<ContactDetails>(instance);
 
        // Wire the ClearNamesCommand from the view to the viewModel.
        CommandManager.RegisterClassCommandBinding(typeof(ContactView),
            new CommandBinding(
                ClearNamesCommand,
                (sender, e) => { viewModel.ClearFullName(); },
                (sender, e) => { e.CanExecute = !String.IsNullOrWhiteSpace(viewModel.FullName); }));
 
        // Wire the NavigateUriCommand from the view to the viewModel.
        CommandManager.RegisterClassCommandBinding(typeof(ContactView),
            new CommandBinding(
                NavigateUriCommand,
                (sender, e) => { viewModel.NavigateTo(e.Parameter); },
                (sender, e) => { e.CanExecute = String.IsNullOrWhiteSpace(viewModel.FullName); }));
 
        DataContext = viewModel;
    }
}

Last edited Dec 18, 2010 at 4:59 PM by baxevanis, version 2

Comments

No comments yet.