How to pass EventArgs to Behaviour?

Topics: Prism v4 - Silverlight 4
Apr 13, 2011 at 6:22 AM

Hi,

<sdk:TabControl>
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <i:InvokeCommandAction Command="{Binding InternalTabsSwitchedCommand}"  />
                </i:EventTrigger>
            </i:Interaction.Triggers>


public DelegateCommand<object> InternalTabsSwitchedCommand { get; private set; }
InternalTabsSwitchedCommand = new DelegateCommand<object>(OnSwitchTab);
public void OnSwitchTab(object args)
	    {
	        
	    }

I was expecting the eventArgs of the SelectionChanged event to be passed into the command. But its always null.  Is this the expected behaviour? There must be a way though.

 

Many Thanks,

Houman

Developer
Apr 13, 2011 at 1:50 PM

Hi Houman,

In order to pass a value to a delegate command's parameter, you need to define the CommandParameter in the XAML. You might find the following blog post useful, as a similar scenario is covered (even though it's not targeted to delegate commands, there should be no difference):

Silverlight Commands Hacks: Passing EventArgs as CommandParameter to DelegateCommand triggered by EventTrigger

I hope you find this helpful.

Guido Leandro Maliandi
http://blogs.southworks.net/gmaliandi

Apr 30, 2011 at 9:30 PM

Hi Guido,

 

This is indeed a great workaround. Why don't you guys include this in the next version? :)

Besides I found a better solution for my case.  This only works thought because I am using a DataGrid. For a ListBox you might have to use the workaround above anyway.
But in case you need the selectedItems of a datagrid you could do this without a need to pass the eventarg to the ViewModel in first place.


<sdk:DataGrid x:Name="call_dataGrid" >
                    <i:Interaction.Triggers>
                        <i:EventTrigger EventName="SelectionChanged">                                                        
                            <i:InvokeCommandAction Command="{Binding SelectedCallsCommand}" CommandParameter="{Binding SelectedItems, ElementName=call_dataGrid}"/>
                        </i:EventTrigger>
                    </i:Interaction.Triggers>
                </sdk:DataGrid>


Your delegateCommand shall accept the type of IList
 
public DelegateCommand<IList> SelectedCallsCommand { get; private set; }

 I have tested this and guarantee its working. :)

Enjoy,
Houman