Is a DelegateCommand the same as an "Attached Behavior"? - wpf

I have been using the DelegateCommand found in the MVVM Visual Studio template at CodePlex. This works very nicely for having Views be able to execute commands on their ViewModel.
I read somewhere that in MVVM "attached behaviors" should be used. As far as I can tell, "attached behaviors" are the same type of pattern as DelegateCommand but are used by Silverlight since it does not have commands.
Is this correct? Or are "attached behaviors" something different in kind and worth learning in addtiion to DelegateCommand?

A DelegateCommand (or a RelayCommand, which is almost the same but accepts a parameter) is just a light-weight implementation of the ICommand interface that allows a ViewModel to expose commands easily.
Attached behaviors are a way to link an event to a command. For instance, most WPF controls don't have a Command property, so you can't normally define a command to react to their events. With attached behaviors, you can "bind" any event of any control to a command of your ViewModel.
Have a look at Marlon Grech's implementation, which seems the most flexible to me

Related

Where do I handle the WPF commands?

I have a whole bunch of RoutedUICommand commands which I fire from different places, using the Command attribute in XAML.
They all are bound right now in my MainWindow.xaml and in my MainWindow.xaml.cs I have a handler for each of them. I have set it up this way, mostly because I resolve the MainWindow class with Unity and it receives all necessary dependencies (i.e. domain services and etc). If I bind the command to a UserControl, I wouldn't have those services available there and also it seems wrong that a UserControl which is given a DataContext, but would be allowed to manipulate its or another context.
My question: does this seem right? To me, something seems off about handling all of the commands in a single central place, especially the main window code behind.
I am new to WPF and can't tell if this is right or wrong. Any advice is appreciated.
You should handle them in your ViewModel. The common pattern in WPF applications is MVVM.
M - model (i.e. DB)
V - view (.xaml files)
VM - a class that is set to be the DataContext for you view. You should do all the logic, binding here.
I suggest having a look at some MVVM frameworks that can simplify the development.
Some of the popular ones are:
Caliburn.Micro
MVVM Light
WPF provides two implementations of the ICommand interface; the System.Windows.Input.RoutedCommand and System.Windows.Input.RoutedUICommand where the latter is a subclass of the former that simply adds a Text property that describes the command.
However, neither of these implementations are especially suited to be used in a view model as they search the visual tree from the focused element and up for an element that has a matching System.Windows.Input.CommandBinding object in its CommandBindings collection and then executes the Execute delegate for this particular CommandBinding.
Since the command logic should reside in the view model, you don’t want to setup a CommandBinding in the view in order to connect the command to a visual element. Instead you should create your own command by creating a class that implements the ICommand. All MVVM libraries out there such as Prism, MvvmLight and Caliburn Micro already have such an implementation. They are usually called DelegateCommand or RelayCommand. Please refer to the following links for more information about commands and how to use them in an MVVM WPF application.
MVVM - Commands, RelayCommands and EventToCommand: https://msdn.microsoft.com/en-us/magazine/dn237302.aspx
Handling events in an MVVM WPF application: https://blog.magnusmontin.net/2013/06/30/handling-events-in-an-mvvm-wpf-application/

MVVM - Pertaining to WPF command binding standards

I think I have a pretty good understanding of the MVVM design model, however I have a quarm with it in regards to WPF, Command bindings and how we are meant to use them.
To bind commands to the XAML directly we are meant to implement the ICommand interface within the ViewModel. Now, the ICommand interface is part of the PresentationCore.DLL, which, correct me if im wrong is part of WPF not the base .NET framework.
Isnt the whole point of the ViewModel and Model that it should be totally UI independant? For example, if I implement ICommand in my ViewModel and use it as a data context in order to bind commands from the XAML, isnt my ViewModel then dependant on the WPF frame work (in particular the PresentationCore.Dll).
What I mean is, if I was to go and try to use my Models and ViewModels in lets say a Windows Forms environment, I would have to reference the PresentationCore.DLL even though I shouldnt need it because im using Windows Forms not the WPF framework.
This seems a bit odd to me, am I missing something here? Is there another way I should be doing it to keep my Model and ViewModel totally UI and UI Framework independant, but still be able to utilise the Command binding in XAML?
Thanks in advance!
I also had this kind of problem but not in wpf but in POCO classes. What i did was I created two partial classes in two different assemblies. Like you create one partial class which is not presentationcore.dll dependent in your VM project and create its partial class in another assembly(say WPFVM) which implements ICommand stuff. Now for Winforms stuff add only VM project reference to View project and for WPF stuff add references of both VM and WPFVM to the View project. I hope this will help.
The point of MVVM is to have the view just be a view, and nothing more. Putting ICommands into the view model helps this as it pulls the code away from the view. Where you will run into problems is if you have to access something on the view that is not a dependency property, which means you can not bind to it.
In my opinion MVVM is very popular with the WPF, Silverlight because it naturally fits into it. The data binding concept in the XAML allows the Views & ViewModels to be bridged using a single property which is the DataContext. As no longer your logic is tied to controls, you get better testability, design-code separation and maintainability. You may be able to implement the MVVM pattern in other places also, but in WPF and Silverlight, it fits so easily due to its data and command binding support. I have read somewhere that, Don't take patterns religiously. They were made to make your life simpler rather than giving you more problems while following it. For Winforms i think there are better patterns, If you are focusing in reusing the business logic, move them out of your ViewModels to seperate classes something like serviceproviders or serviceagents and share them between your Winforms and WPF apps.
This has changed in .NET 4.5 compare
.NET Framework 4.5
.NET Framework 4

WPF RibbonApplicationMenuItem Command Binding

Build a menu using the WPF Ribbon. My application is in the MVVM pattern. To connect the RibbonButton the ViewModel, use the form:
Command="{Binding Logoff}"
It works correctly.
Now, using the same command on RibbonApplicationMenuItem:
Command="{Binding Logoff}"
does not raise any calls or event. What do I need for the RibbonApplicationMenuItem call a method that is in the ViewModel? When I create methods in the codebehind, it works. But this goes against what I'm developing MVVM architecture.
Would have any suggestions to make the RibbonApplicationMenuItem be connected via binding to ViewModel?
After several searches, I found the solution to the problem. Ribbon in the package has the file MicrosoftRibbonForWPFSourceAndSamples, which has a great example of how to work with RibbonApplicationMenuItem. I used the method DelegateCommand, which comes as an example in this package.
I used the following syntax in the constructor of View:
DelegateCommand = new rbiEfetuarLogoff.Command(mainWindowViewModel.DoLogoff);
I hope it's useful for someone else.
Best Regards

Reuse controls inside a usercontrol

I have a UserControl UserControl1 and a button inside the UserControl1. And I have a UserControl1ViewModel that has an ICommand property for the button. Using this command I need to call a method outside(from other VMs or VM of the MainWindow) the VM. What is the best practice for this?
You might want to examine MVVM lite by Laurent Bugnion http://www.galasoft.ch/mvvm/getstarted/
This is a lightweight toolkit for helping enforce mvvm concepts. In it, every viewmodel is a static member in a ViewModelLocator class. So for instance, in your command you could do something like this.
ViewModelLocator.MainViewModel.MainContent = NewContent;
You can totally do this without mvvm lite, but using it really helps speed up the learning curve and enforce modularity.
You're most likely looking to implement the Mediator pattern to handle the communication between two viewmodels.
Another SO question along the same vein is:
mvvm-view-model-view-model-communications
I would consider using Controllers for the mediation between the ViewModels. The WPF Application Framework (WAF) shows how this works.

What is the best approach to binding commands in a ViewModel to elements in the View?

Anyone who has tried to implement RoutedCommands in WPF using M-V-VM has undoubtedly run into issues. Commands (non-UI commands that is) should be implemented in the ViewModel. For instance if I needed to save a CustomerViewModel then I would implement that as a command directly on my CustomerViewModel. However if I wanted to pop up a window to show the users addresses I would implement a ShowCustomerAddress command directly in the view since this a UI specific function.
How do I define the command bindings in the viewmodel, and use them in the view?
Here is a solution that I came up with.

Resources