0

我是 XAML 的新手,请帮助我为什么无法获得点击事件。

我可以通过需要单击项目的确切 ID 在 DataTemplate/Grid 上获取 Tapped 事件。这是我的网格:

 <GridView                            
          x:Name="newsGridView"
          SelectionMode="None"
          IsSwipeEnabled="false"
          ItemsSource="{Binding Source={StaticResource newsViewSource}}"
          ItemClick="newsGridView_ItemClick">
          <GridView.ItemTemplate>
          <DataTemplate>
          <Grid HorizontalAlignment="Left" Width="160" Height="160">
          <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
          <Image Source="{Binding Image}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Text}"/>
          </Border>
          <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
          <TextBlock Text="{Binding Text}" Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" Style="{StaticResource TitleTextStyle}" Height="30" Margin="15,0,15,0"/>
          <TextBlock Text="{Binding Subtitle}" Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" Style="{StaticResource CaptionTextStyle}" TextWrapping="NoWrap" Margin="15,0,15,10"/>
          </StackPanel>
          </Grid>
          </DataTemplate>
          </GridView.ItemTemplate>
          </GridView>

我的事件处理程序

 private void newsGridView_ItemClick(object sender, ItemClickEventArgs e)
 {
    var s = sender as NewsObject;           
 }

TIA

4

1 回答 1

1

你必须分配IsItemClickEnabled财产

<GridView                            
          x:Name="newsGridView"
          SelectionMode="None"
          IsSwipeEnabled="false"
          ItemsSource="{Binding Source={StaticResource newsViewSource}}"
          IsItemClickEnabled="true"
          ItemClick="newsGridView_ItemClick">
               <GridView.ItemTemplate>
                    <DataTemplate>
                    <Grid HorizontalAlignment="Left" Width="160" Height="160">
                    <Border Background="{StaticResource ListViewItemPlaceholderBackgroundThemeBrush}">
                         <Image Source="{Binding Image}" Stretch="UniformToFill" AutomationProperties.Name="{Binding Text}"/>
                    </Border>
               <StackPanel VerticalAlignment="Bottom" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
                    <TextBlock Text="{Binding Text}" 
                    Foreground="{StaticResource ListViewItemOverlayForegroundThemeBrush}" 
                    Style="{StaticResource TitleTextStyle}" Height="30" Margin="15,0,15,0"/>
                    <TextBlock Text="{Binding Subtitle}" 
                    Foreground="{StaticResource ListViewItemOverlaySecondaryForegroundThemeBrush}" 
                    Style="{StaticResource CaptionTextStyle}" 
                    TextWrapping="NoWrap" Margin="15,0,15,10"/>
               </StackPanel>
          </Grid>
     </DataTemplate>
     </GridView.ItemTemplate>
</GridView>
于 2012-11-22T20:49:23.503 回答