在DataTemplate中单击项目时禁用ListView的ItemClick(WP 8.1)

时间:2014-09-16 01:55:04

标签: c# windows xaml winrt-xaml windows-phone-8.1

我有ListView

<ListView x:Name="lstBWDetails" Grid.Row="2" Width="{Binding ActualWidth,ElementName=MainGrid}"
    ItemTemplate="{StaticResource ItemFavoriteWord}"
    ItemClick="lstBWDetails_ItemClick" IsItemClickEnabled="True"
    Tapped="lstBWDetails_Tapped"  >              
</ListView>

和DataTemplate

<DataTemplate x:Key="ItemFavoriteWord">
    <Grid Width="400" Margin="10 0 5 2" Height="50" Background="#f0f0f1" >
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="100" />
        </Grid.ColumnDefinitions>
        <StackPanel  Margin="0 0 0 0" Orientation="Vertical" HorizontalAlignment="Left" Grid.Column="0" Tapped="StackPanel_Tapped">
            <TextBlock  HorizontalAlignment="Left" VerticalAlignment="Center" Text="{Binding en}" Tag="{Binding en}"   TextWrapping="Wrap" FontSize="18" Foreground="#c8454d" Margin="15 0 0 0" />
            <TextBlock  HorizontalAlignment="Left" VerticalAlignment="Center" Text="{Binding local}" Tag="{Binding en}" TextWrapping="Wrap" FontSize="18" Foreground="#0577bb" Margin="15 0 0 0" />
        </StackPanel>
        <StackPanel Margin="0 0 10 0" Orientation="Horizontal" HorizontalAlignment="Right" Grid.Column="1" >
            <Image Height="40" Name="imgFav"  Width="50" Margin="0 0 10 0" Tapped="imgFav_Tapped"  Source="{Binding favo}" Tag="{Binding id}"  />
        </StackPanel>
        <Border Grid.ColumnSpan="2" Grid.Column="0" BorderBrush="#FFC9C9C9" BorderThickness="0,0,0,0.5" >
        </Border>
    </Grid>
</DataTemplate>

当我点击ListView项目点击 - &gt;好 但是当我点击imgFav_tapped时 - > ListView项目点击首先激活,第二个imgFav_Tapped激活第二个

所以我想在我点击imgFav_Tapped

时禁用ListView项目

抱歉,我的英文不好

1 个答案:

答案 0 :(得分:0)

是的,来过这里!我相信如果您设置一个列表以启用itemclick,您将始终获得整个点击,绑定到列表项的上下文,并且它将始终优先于您想要对模板内的项目执行的任何其他操作。< / p>

您可以尝试在模板中的单个控件内处理click事件,但这可能不是您想要的...

我通常解决这个问题的方法是禁用itemclick,而是处理Tapped事件,通过OriginalSource将任何对象转换为FrameworkElement,这样我就可以提取datacontext并尝试对实际点击的项目作出适当的反应。

类似这样的事情

    private void List_Tapped(object sender, TappedRoutedEventArgs e)
    {
        var source = e.OriginalSource as FrameworkElement;
        if (source == null) return;

        var item = source.DataContext as MyItemClass;
        if (item == null) return;

        // do stuff with your item
     }

不是一个完全直截了当的解决方案,但我希望这对您更接近您所寻找的内容有所帮助!