Grid" Tap"或者"点击"事件通过数据模板?

时间:2013-09-28 21:36:55

标签: c# command windows-phone

我已经尝试在网络上搜索Windows Phone中的命令,但我找不到符合我问题的方案。我有DataTemplate创建了一些网格。对于这些网格,我希望他们在触发Click事件时执行某些操作。但是, Grid元素没有Command属性

我不需要通过命令来完成它,但我认为这是要走的路。

这是我想做的事情

<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>

1 个答案:

答案 0 :(得分:2)

要将命令绑定到网格,您需要使用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>

使用以下命名空间定义:

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