3

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

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

When I'm click in ListView Item Click -> OK But when I'm click in imgFav_tapped -> The ListView Item Click be actived first , second imgFav_Tapped be actived second

So I want disable ListView Item Click when i click at imgFav_Tapped

Sorry my bad English

Chubosaurus Software
  • 8,133
  • 2
  • 20
  • 26
EddyLee
  • 803
  • 2
  • 8
  • 26
  • Have you tried interaction triggers? This post might help you out -> http://stackoverflow.com/questions/5545187/wpf-how-to-bind-a-command-to-the-listboxitem-using-mvvm – aggietech Sep 16 '14 at 02:13

1 Answers1

0

yep, been here! i believe if you set a list to enable itemclick you will ALWAYS get the click as a whole, bound to the context of the list item, and it will ALWAYS take precedent over anything else you want to do with the items inside the template.

you can try to handle the click event inside individual controls within the template but this is probably not what you want...

the way I usually solve this is to disable itemclick and instead handle the Tapped event, casting whatever object via the OriginalSource to FrameworkElement, which lets me extract the datacontext and try to react appropriately to the actual clicked item.

something like this

    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
     }

not an entirely straightforward solution, but I hope this is helpful to you in getting closer to what you're looking for!

SelAromDotNet
  • 4,715
  • 5
  • 37
  • 59