How to bind to Command on a single control?

Oct 27, 2012 at 4:53 AM

In samples, I saw we can use the follow statement to bind an Event to a Command:

            <GridView ...

            View:AttachedEventCommand.Event="ItemClick"
            View:AttachedEventCommand.Command="{Binding ItemClickCommand}" >

But how can I bind another Command to another Event, such as SelectionChanged on this control too?

Thanks.

Coordinator
Oct 27, 2012 at 1:29 PM

At this point its not possible to hook two commands with that syntax. However there is a new syntax as off 2.0.3 that allows you to wire N number of events to N number of handlers. You can still use DelegateCommand with things like buttons but ultimately the ICommand interface doesn't offer any advantages when you are talking about handling an event (enabling and disabling have no value) and is chaff. So for normal event handling the ICommand interface is bypassed and your handler method will be called directly.

The short hand version of hooking up events looks like this

<GridView .... View:EventHandlers.Attach="ItemClick => ItemClickHandlerMethod($eventArgs); SelectionChanged => SelectionChangedHandler($eventArgs); etc..." />

The long hand version looks like this

<GridView ... >

  <View:EventHandlers.List>

     <View:EventHandlerList>

        <View:EventHandlerInstance Attach="ItemClicked => ItemClickHandler($eventArgs)" />

        <View:EventHandlerInstance Attach="SelectionChanged => SelectionChangedHandler($eventArgs)" />

     </View:EventHandlerList>

  </View:EventHandlers.List>

</GridView>

In your ViewModel your handler method signatures would look like this

public void ItemClickHandler(object eventArgs) { ... }

public void SelectionChangedHandler(object eventArgs) { ... }

 

You can control how many args are passed to your method and in what order by changing the parameters in the Attach statement. So for example if you write:

View:EventHandlers.Attach="Click => ClickHandler()" then your method signature should take no parameters.

View:EventHandlers.Attach="Click => ClickHandler($sender, $datacontext, $eventArgs)" then your method needs 3 parameters one for the event sender, the data context from where the event occurred and one parameter for the event args.

 

There are some examples of this in the new Example App but I will also go update the Simple Weather Example to no use the deprecated syntax.

Sorry for the confusion, I'm working on documentation over the coming weeks and this will be an area I cover. If you have anymore questions don't hesitate to post them in the discussion or contact me directly (although the former is probably better in case other people have the same question).

Oct 30, 2012 at 1:25 PM

It's great, thanks so much and I introduced and recommaned your StyleMVVM codeplex project in a MS win8 Dev session today.