I'm creating a WP8.1 app using C# and XAML
I have a ListView in which the ListViewItems contains a TextBlock.
The TextBlocks have Tapped="" event handlers, however I want to stop the cell being selected when one of these TextBlocks are tapped.
Setting 
TappedRoutedEventArgs.Handled = true;
is not stopping the ListViewItem being selected.
(Because of the hierarchy I cannot traverse throught the TextBlocks parent elements to get the ListViewContainer).
What would be the best way to go about this?
Ok, here's how I solved this problem.
XAML:
<ListView IsItemClickEnabled="True" ItemClick="ItemClickHandler" SelectionMode="None">
   <ListView.ItemTemplate>
      <DataTemplate>
         <TextBlock Text="SomeText" Tapped="TextTapped" />
      </DataTemplate>
   </ListView.ItemTemplate>
</ListView>
C#
private bool IsItemClickDisabled;
private void TextTapped(object sender, TappedRoutedEventArgs e)
{
   IsItemClickDisabled = true;
   //TODO: Your tap event handler
   e.Handled = true;
}
private async void ItemClickHandler(object sender, ItemClickEventArgs e)
{
   IsItemClickDisabled = false; //Reset each time
   await System.Threading.Tasks.Task.Delay(TimeSpan.FromMilliseconds(1));
   if (!IsItemClickDisabled)
   {
      //TODO: ItemClick handler
   }
}
So, generally, ItemClickHandler will be executed first, but we delay this event, so another events can execute. If any tapped event was executed, we just say: "Ok, we gonna execute this code, no need to execute ItemClick event."
Use property IsHitTestVisible="False" on the control. Mouse events will pass through it.
I had the same problem with a custom control that was on a ListViewItem. I overrode the OnPointerPressed event and mark it as handled in addition to the tapped event and this stopped the listview from receiving the selection event.
protected override void OnPointerPressed(PointerRoutedEventArgs e)
{
  base.OnPointerPressed(e);
  e.Handled = true;
}
The above will work when deriving your own control, but you can just hook the events on any control and mark them as handled something like
<TextBlock Text="whatever" Tapped="UIElement_OnTapped"
    PointerPressed="UIElement_OnPointerPressed"/>
and then in code
private void UIElement_OnTapped(object sender, TappedRoutedEventArgs e)
{
  e.Handled = true;
}
private void UIElement_OnPointerPressed(object sender, PointerRoutedEventArgs e)
{
  e.Handled = true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With