Disable ItemClick from ListView when element is clicked in DataTemplate (WP 8.1)
I have a 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 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 click a ListView item -> OK But when I click imgFav_tapped -> ListView item Click to activate first, the second imgFav_Tapped is activated second
So, I want to disable the ListView item when I click imgFav_Tapped
Sorry my bad english
source to share
Yes here! I believe that if you set the list to include itemclick, you will ALWAYS get the click as a whole, bound to the context of the list item, and it will ALWAYS take precedence over anything else you want to do with the elements inside the template.
you can try handling the click event inside the individual controls in the template, but that's probably not what you want ...
the way I usually solve this is to disable itemclick and instead handle the Tapped event, discarding any object through the SourceSource in the FrameworkElement, which allows me to fetch the datacontext and try to react appropriately to the actual click.
something like that
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 exactly an easy solution, but I hope it helps you get closer to what you are looking for!
source to share