Grid "Tap" or "Click" event through a data template?

I have already tried searching the web for using commands in Windows Phone, but I can't find a scenario that fits my problem. I have a DataTemplate which creates some grids. For these grids, I want them to do something when their Click event is triggered. However, the Grid element doesn't have a Command property.

I don't nescessarily need to do it through commands, but I thought that was the way to go.

Here's what I want to do.

<ItemsControl VerticalAlignment="Top" Visibility="Collapsed" x:Name="RadioList">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Margin="12" Tag="{Binding}">
                <!-- this is the grid I want to listen for clicks on -->
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

Upvotes: 0

Views: 824

Answers (1)

Benoit Catherinet
Benoit Catherinet

Reputation: 3345

To bind a command to a grid you will need to use EventTrigger:

<ItemsControl VerticalAlignment="Top" Visibility="Collapsed" x:Name="RadioList">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <Grid Margin="12" Tag="{Binding}">
                <i:Interaction.Triggers>
                                <i:EventTrigger EventName="Tap">
                                    <i:InvokeCommandAction Command="{Binding YourCommand}" />
                                </i:EventTrigger>
                            </i:Interaction.Triggers>
            </Grid>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

with the following namespace definition:

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"

Upvotes: 2

Related Questions