Hi i have recently looked into WPF and started learning about Events and Commands. I typically use Commands on Button clicks which causes a method to Run in my "view model".
Is it possible to make the Button react to any other events like the MouseOver event through the use of commnds? Or would WPF Events be used in this case?
If WPF Events are to be used, then should the event handler implementation just call a method in the View Model to keep concerns sperate?
In a WPF application, events are often implemented as a tunneling/bubbling pair. So, you'll have a preview MouseDown and then a MouseDown event. Given below is a simple example of a Routed event in which a button and three text blocks are created with some properties and events.
Commanding is an input mechanism in Windows Presentation Foundation (WPF) which provides input handling at a more semantic level than device input. Examples of commands are the Copy, Cut, and Paste operations found on many applications.
Routed commands give you three main things on top of normal event handling: Routed command source elements (invokers) can be decoupled from command targets (handlers)—they do not need direct references to one another, as they would if they were linked by an event handler.
The RelayCommand and RelayCommand<T> are ICommand implementations that can expose a method or delegate to the view. These types act as a way to bind commands between the viewmodel and UI elements.
This is a fair question, and one that is a common, yet "solved" (debatably) problem within the MVVM architecture realm. If you are using an MVVM framework, you are likely to find something similar to the EventToCommand Behavior, here is the sample from the MVVM Light Toolkit.
In short, this allows you to map an event to a command binding like so:
<Rectangle Fill="White" Stroke="Black" Width="200" Height="100"> <i:Interaction.Triggers> <i:EventTrigger EventName="MouseEnter"> <cmd:EventToCommand Command="{Binding TestCommand, Mode=OneWay}" CommandParameter="{Binding Text, ElementName=MyTextBox, Mode=OneWay}" MustToggleIsEnabledValue="True" /> </i:EventTrigger> </i:Interaction.Triggers> </Rectangle>
Update:
There are two other "reasonable" solutions to this problem:
One uses the now considered legacy "AttachedCommandBehavior" extension found here.
The other is a little bit irritating, but workable.
This looks gross but I'm fairly certain is actually a bit faster than just using traditional command bindings. In order to be sure I'd need to see the IL, and I don't think that it matters in this case.
/Update
I want to note however that this is not always an ideal situation. I've discovered that more often than not, I'm using EventToCommand to cover a design issue. Please consider the following:
Most importantly perhaps is that you remember that you are the developer. Guidelines in themselves do not solve problems, but consideration of guidelines may make the solution to a problem apparent.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With