How to get ClickedItem from GridView?

Feb 1, 2014 at 4:47 PM
In my View I have a GridView:
<GridView x:Name="mainGridView" ItemsSource="{Binding Posts}" IsItemClickEnabled="True" SelectionMode="None" Grid.Row="1">
            <triggers:Interactions.Triggers>
                <triggers:EventTrigger EventName="ItemClick">
                    <triggers:InvokeCommandAction Command="{Binding GridViewItemClickCommand}"
                                          PassEventArgsToCommand="True"/>
                </triggers:EventTrigger>
            </triggers:Interactions.Triggers>
            <GridView.Resources>
                <DataTemplate x:Key="mainGridViewStyle">
                    <Grid x:Name="gridItem" Width="320" Height="240">
                        <Image x:Name="imageItem" Source="{Binding preview_url}" Stretch="UniformToFill"/>
                    </Grid>
                </DataTemplate>
            </GridView.Resources>
            <GridView.ItemTemplate>
                <StaticResource ResourceKey="mainGridViewStyle"/>
            </GridView.ItemTemplate>
        </GridView>
In my ViewModel I have a command:
public DelegateCommand GridViewItemClickCommand { get; private set; }
In my ViewModel constructor I have command initialization:
GridViewItemClickCommand = new DelegateCommand(() => navigationService.Navigate("Post",null));
How can I get ClickedItem after tapping/clicking GridView item?
Feb 4, 2014 at 6:15 PM
Hi,

I am not aware of the implementation of the InvokeCommandAction you are using but if that action will pass the EventArgs of the event as the command parameter, then a possible way to recieve these args in the view model is to use the generic implementation of the DelegateCommand. The generic implementation allows you to recieve a parameter that will be passed to the "delegate" of the DelegateCommand. For example, if you redefine your DelegateCommand like this:
public DelegateCommand<ItemClickEventArgs> GridViewItemClickCommand { get; private set; }
Then you would be able to recieve the parameters like this (where e are the ItemClickEventArgs of the event):
this.GridViewItemClickCommand = new DelegateCommand<ItemClickEventArgs>( (e) => { /* Some code */ });
I hope this helps,

Damian Cherubini
http://blogs.southworks.net/dcherubini
Feb 4, 2014 at 6:55 PM
Thanks for your answer. I solved my problem by calling a method despite of using command.
I added this behavior to XAML element:
            <Interactivity:Interaction.Behaviors>
                <Core:EventTriggerBehavior EventName="ItemClick">
                    <Core:CallMethodAction MethodName="GridViewClick" 
                TargetObject="{Binding Mode=OneWay}" />
                </Core:EventTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
Then I added corresponding method to ViewModel:
public async void GridViewClick(object sender, object parameter)
{
var arg = parameter as ItemClickEventArgs;
// some code
}