When to use event and command for WPF / MVVM?

I am writing a WPF application with an MVVM pattern. So far I have not used the command in my code. In my view model, I implement INotifyPropertyChanged and used (the PropertyChangedEventHandler PropertyChanged event) to fire events. Why do I feel like I'm still missing out on some kind of WPF concept on how to use the command?

When is it appropriate to use commands?

+4
source share
2 answers

Commands in WPF are used for an abstract action initiated by the user (for example, pressing a Button or pressing a key.

here is a basic example:

Suppose you want to search for employees in your database when the user clicks the Search button or presses the enter key while focusing the search box.

You can define your ViewModel as follows:

 public class MyViewModel: ViewModelBase { public string SearchText {get;set;} //NotifyPropertyChanged, etc. public Command SearchCommand {get;set;} public MyViewModel() { //Instantiate command SearchCommand = new DelegateCommand(OnSearch); } private void OnSearch() { //... Execute search based on SearchText } } 

And your view:

 <StackPanel> <TextBox Text="{Binding SearchText, UpdateSourceTrigger=PropertyChanged}"> <TextBox.InputBindings> <KeyBinding Key="Enter" Command="{Binding SearchCommand}"/> </TextBox.InputBindings> </TextBox> <Button Content="Search" Command="{Binding SearchCommand}"/> </StackPanel> 

Notice how the KeyBinding and Button Command properties KeyBinding bound to the same command ( SearchCommand ) in the ViewModel. This facilitates reuse, and also helps to preserve the actual logic in the ViewModel with all its goodness (testability, etc.), while maintaining the purity and lack of code.

+11
source

The most interesting thing is that the use of the original Commands concept from WPF is absolutely not required :). You could build large and complex applications with all this beauty of loosely coupled design (xaml) and business logic (C # / vb code) in place using only MVVM and the free open source framework library Caliburn.Micro .

Disclaimer I am just a happy user of this library and have nothing to do with its creators, so these are not paid ads or anything like that.

Please just view this very simple sample from the official documentation :

-> Basic configuration, actions and conventions <-

and you can fill in strong binding events from your XAML view directly to the methods in your C # view model without a mess of proxy code for declaring commands and registering (for example, this is implemented in other similar applications).

And it doesn't matter that this example is a Silverligh application - Caliburn.Micro supports all major Xaml platforms in almost the same way, and the WPF sample will look the same as above, based on Silverlight.

In addition to the mentioned main features (binding to methods) Caliburn.Micro have:

  • handy predefined naming conventions for binding that leave your XAML files clean and readable (and yet design friendly)!
  • basic implementation of INotifyPropertyChanged so you can simply inherit all your view models from it
  • basic classification for implementing commonly used scenarios, such as:
    • master part
    • parent-child
    • list with selected items
  • EventAggregator for an even more loosely coupled relationship between view models and other classes.
  • loosely coupled support for things like keyboard focus and window management (for WPF)
  • and a little more :)

Just give it a shot and you will never need WPF vanilla commands;)

0
source

Source: https://habr.com/ru/post/1485535/


All Articles