Click a text block without selecting a parent list item

I am developing a Windows storage application with C# and XAML . I am using ListView to display a collection of data.

Inside the ListView , I have a data template that has grids and a TextBlock in the grid. I want to click / click on a TextBlock and give an action without selecting the parent ListView , since I already have an event to handle the selected ListView . I do not want both to overlap.

Thanks in advance for any response.

+4
source share
1 answer

So, you want to be able to select ListViewItem when you click on one part of it, but not TextBlock ? If so, in the TextBlock Tapped event add e.Handled = true; . This should make sure that it is not redirected to the parent ListView .

Another thing you can do (which is likely to be a more general solution for everything you want to do with ListViewItems) is not to use the SelectionChanged event, but instead handle everything with ItemClick . You can then determine if the OriginalSource this event is really your TextBlock . Then, if it is not a TextBlock , change the parent ListView SelectedItem .

An example to correct the source

 public static void ItemClickEvent(object sender, ItemClickEventArgs e) { if(e.OriginalSource is TextBlock) DoNothingOrMaybeTextBlockEvent(); else { ListView.SelectedItem = e.ClickedItem; } } 

Hope this helps.

Edit: added sample code to verify source code

0
source

Source: https://habr.com/ru/post/1490934/


All Articles