简体   繁体   中英

Is there anyway to plumb a command not via the viewmodel in wpf?

This question is simple but I fail to accurately get a hold of how to do it.

I decided to use WPF, but I don't want to implement some of MVVM pattern in the classic way.

In my project I decided to create my models with INPC and to use them as feed to my XAML as if they were View-Model as well.. so instead of having:
View(<-Binding->)View-Model(<-INPC->)Model

what I designed is:
View(<-Binding->)Model.

The constrain of course is that the model must implement INPC, however compared to classic MVVM in practice - model also need to implement INPC - so I find my this to be perfectly alright as a productive shortcut guided by DRY principle.

I want to support commands as well, I like the concept of commands and I understand how to implement commands when using a view-model. however as I described I decided to skip the view-model.

So providing I have the command written as follows:

public class MyCommand : ICommand
{
    public event EventHandler CanExecuteChanged;

    public void Execute(object parameter)
    {
    //do somthing
    }

    public bool CanExecute(object parameter)
    {
        return true;
    }
}

And that my XAML looks somthing like that:

<UserControl x:Class=... blah blah...
 xmlns:Model="clr-namespace:MyProject.BusinessLogic.Person"
 xmlns:Commands="clr-namespace:MyProject.Commands">

<TextBox Text="{Binding FirstName}" />
<Button Command="What do I put here?" />

<UserControl>

So, what do I put in button command to allow it to invoke MyCommand (which is not in the Model) ?

You do want a view model, and that's where your presentation logic should go, including commanding.

Your models obviously can't have any reference to ICommand etc. as it sits in a System.Windows.Input namespace, so you need a type that bridges the model with the view, which is the view model.

You can expose the model as a property on your view model if you don't wish to follow the pattern entirely and want to avoid delegating all view model calls to the model.

Another consideration is that if you're doing MVVM then you really should be using an MVVM framework . They will provide an alternative to commanding, which has limitations.

For example, if you wish to re-imagine your UI so that a Button is hidden when it is not enabled, then you can't achieve this easily with commanding.

An MVVM framework such as Caliburn.Micro provides Actions , which provide many benefits over commanding.

You might want to read this: http://wpfglue.wordpress.com/2012/05/07/commanding-binding-controls-to-methods/

The idea is to have reusable components which allow to create a CommandBinding which calls a method in the model, and can be configured in XAML. So, the "plumbing" would be done by these components, while the Model would only implement business-related methods.

This can be generalized into something I call a ViewModelKit, ie a set of components which can be assembled as resources in XAML and cover common jobs of a ViewModel.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM