网格"Tap"还是通过数据模板"Click"事件?



我已经尝试在 Windows Phone 中搜索 Web 以使用命令,但我找不到适合我问题的场景。我有一个创建一些网格的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>

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

最新更新