This project is read-only.

Clarification on adding events to views

Jul 1, 2013 at 6:24 PM

I wanted to confirm the process I have for adding support for events (rightMouseClick, MouseOver, whatever) to a view where I make the best use of the FW approach.

Using the RightClick event as an example, I believe I do the following:

In the model, create a ViewAction property to hold a delegate to the event. So I would have something like:
public ViewAction RightClick
In the constructor, I would assign something to that property:
RightClick = new ViewAction("Right Click", execute: (a, o) => Controller.Message("You Right Clicked!"));
RightClick = new ViewAction("Right Click", execute: this.RightClickMethod() );
Now that we have something to do the work, we need to call it, so in the view, I simply add the following to whatever should support the rightclick. So if I wanted an address lookup option available via a right click, I would have the following
<TextBox Text="{Binding Address}" 
            mvvm:View.WidthEx="30" >
        <Controls:EventCommand Command="{Binding RightClick}" 
                Event="MouseRightButtonDown" />
This would then bind the RightClick method of the model to the MouseRightButtonDown event in the view.

Am I missing anything? Or is there additional information I might want to know about this?

I assume that if I wanted this to happen on some other event, I would just change the Event="" statement and now the code would work with that event instead.

Using this approach, if I wanted to tie 2 (or more) events to one method, I assume I would have multiple Controls references withing the TextBox element i.e.:
<TextBox Text="{Binding Address}" 
            mvvm:View.WidthEx="30" >
        <Controls:EventCommand Command="{Binding RightClick}" 
                Event="MouseRightButtonDown" />
        <Controls:EventCommand Command="{Binding MiddleClick}" 
                Event="MouseRightButtonDown" />

If I want a generic rightClick added to both the form and all the controls on the form, where I could override the right click for some controls (say I wanted additional options), what would be the best way to do it?

And since I would want this capability on all my edit forms, could I embed it in a style?


Jul 1, 2013 at 9:59 PM
You got that pretty much correct.

If you want to bind different events to the same command, you don't have to create two commands. You can just bind them to the same command. Similarly, if you want to bind the root of a view to a right-click command, you can bind that and it should bubble through from all other controls as long as there isn't anything in the controls that eats that event.

Note: In many cases, you probably wouldn't do something quite as specific as "RightClickCommand", but instead have a command that serves a certain purpose. Such as a "ShowSecondaryOptions" command, which can then be bound to the right-click event. Plus potentially other things, such as a key combination. Or, in a different theme, maybe you have a touch-and-hold gesture in a touch-screen system.

And yes, you can absolutely define these event handlers in styles also. So to do the same as in your example above in a style, you'd do something like this:
<Style TargetType="TextBox">
    <Setter Property="Controls:Ex.EventCommand">
            <Controls:EventCommand ... />