locked
ListView event, intermittent behavior RRS feed

  • Question

  • I have a list view in the XAML of a page, its bound to a list and populates at runtime. Its populated from a collection of objects, these have a ToString() override in then so I've assumed that's being called and the list view is adding TextBlock instances as it build the view.

    Sometimes when I click on an item on the screen the click event handler shows a TextBlock as the 'OriginalSource" but at other times its a ListViewItemPresenter - so code that casts this object fails fromn time to time. Why does this happen?

    When the clicked item get raised by the event as a ListViewItemPresenter all is well, the Content property is the Container object, but when its a TextBox Content is just text.

    The event I'm trapping is Tapped incidentally, I tried ItemClick but that never seems to get raised.

    Cap'n



    Saturday, November 2, 2013 3:46 PM

Answers

All replies

  • For ItemClick see http://msdn.microsoft.com/en-us/library/windows/apps/hh780625.aspx . You have to enable it.

    For Tapped, OriginalSource will be the actual element tapped. If you tap the TextBlock its the TextBlock. If you tap the area around the TextBlock it will be the panel behind it.

    Sunday, November 3, 2013 2:25 PM
    Moderator
  • For ItemClick see http://msdn.microsoft.com/en-us/library/windows/apps/hh780625.aspx . You have to enable it.

    For Tapped, OriginalSource will be the actual element tapped. If you tap the TextBlock its the TextBlock. If you tap the area around the TextBlock it will be the panel behind it.

    Yes it's looking like that, the event details depend on where the user clicks. So does one write event handler code that must determine what kind of element was clicked by the user? What's the recommended policy for simply ascertaining the bound object irrespective of which element the user clicks? Having to code different logic for when a user clicks text from when a user clicks empty space next to that text surely isn't the way this is done?

    Thanks.

    Cap'n


    Monday, November 4, 2013 5:30 AM
  • The point of the OriginalSource is so you can get the actual item tapped before it routed. If you want the logical source (i.e. the control on which you set the event) then use the sender.

    --Rob

    Tuesday, November 5, 2013 4:55 AM
    Moderator