Why doing it the hard way?
You can change the DataTemplate
in a way that each ListViewItem
gets divided into two parts, "change amount part" and "item's info part" :
<ListView ItemClick="OnItemClick">
<ListView.ItemContainerStyle>
<Style TargetType="ListViewItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="ListViewItem">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="*"/>
<ColumnDefinition Width="100"/>
</Grid.ColumnDefinitions>
<!-- This is the item's info part -->
<StackPanel Orientation="Horizontal" Grid.Column="0" >
<TextBlock Text="{Binding Title}" />
<TextBlock Text="{Binding Qnty}" />
</StackPanel>
<!-- This is the change amount part -->
<StackPanel Tag="Oops!" Orientation="Horizontal" Grid.Column="1" >
<Button Content="▲" />
<Button Content="▼" />
</StackPanel>
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ListView.ItemContainerStyle>
</ListView>
Now you can try one of the following ways to handle ItemClick
properly:
First solution: You can find out what was the OriginalSource that raised the ItemClick
event. if it was the "change amount part", then user probably tapped it by mistake and the event should be cancelled, if it was "item's info part" then the event should be raised:
private void OnItemClick(object sender, ItemClickEventArgs e)
{
var tag = ((FrameworkElement)e.OriginalSource).Tag;
if (tag != null && tag.ToString() == "Oops!")
{
return;
}
//else: do whatever needs to be done on item click
}
Second solution:
You can set the ListView
to SelectionMode="None"
and IsItemClickEnabled="False"
, and then I added Tapped handlers for each element manually (e.g. ItemTapped
for item's info part and OnIncreaseTapped
and OnDecreaseTapped
for the buttons):
<ListView ItemClick="OnItemClick" SelectionMode="None" IsItemClickEnabled="False">
...
<!-- This is the item's info part -->
<StackPanel Orientation="Horizontal" Tapped = "OnItemTapped" Grid.Column="0" >
<TextBlock Text="{Binding Title}" />
<TextBlock Text="{Binding Qnty}" />
</StackPanel>
<!-- This is the change amount part -->
<StackPanel Tag="Oops!" Orientation="Horizontal" Grid.Column="1" >
<Button Content="▲" Tapped = "OnIncreaseTapped"/>
<Button Content="▼" Tapped = "OnDecreaseTapped"/>
</StackPanel>
</Grid>
Third solution:
Why even let user to tap the wrong area? I'd rather get rid of this area by using an ItemTemplate
like this:
instead of this: