locked
Detect what has been selected on a Listbox DataTemplate RRS feed

  • Question

  • Hello , 

    I have a Listbox which is bonded to a list of an object that holds two elements : a string and an image

    I want to detect if the user taps on the image or on the textblock

    I tried this :

    this is my xaml :

     

    <ListBox x:name = "TheList"  ItemSource ="{Binding List}" SelectionChanged="SelectionChanged_1">
    
    
    <DataTemplate>
    
    <TextBlock Text = "{Binding Name}" />
    <Image ImageSource = "{Binding Picture}" />
    
    <StackPanel>
    
    <\StackPanel>
    
    <\DataTemplate>
    
    
    
    
    </ListBox>

    And here is my code behind :

    private void SelectionChanged_1(object sender, SelectionChangedEventArgs e)
            {
    
    
    
    
       if (TheList.SelectedItem != null)
                {
    
                    if(e.OriginalSource == sender as  TextBox ){
    
    
                        MessageBox.Show("Text");
    
                    }  else if(e.OriginalSource  == sender as Image ){
    
    
                        MessageBox.Show("Image");
    
                    }
    }
    }

    But it does not work if i tap on the image the MessageBox shows "Text"

    How can I detect if the image or the textblock has been tapped?

    Tuesday, January 21, 2014 11:32 PM

Answers

  • in the tap method...

    var myTappedobject = (Sender as FrameworkElement).DataContext as WhateverObjectYouBindTo.

    • Marked as answer by Lel14 Wednesday, January 22, 2014 4:28 PM
    Wednesday, January 22, 2014 4:17 PM

All replies

  • Put a tap event in the image and a tap event in the textblock and handle them accordingly.
    Wednesday, January 22, 2014 8:31 AM
  • But then how can I detect which Item of the list has been chosen ? 
    Wednesday, January 22, 2014 3:40 PM
  • in the tap method...

    var myTappedobject = (Sender as FrameworkElement).DataContext as WhateverObjectYouBindTo.

    • Marked as answer by Lel14 Wednesday, January 22, 2014 4:28 PM
    Wednesday, January 22, 2014 4:17 PM
  • Yes it works thank you very much !

    But I  am very curious about why my method did not work and yours succeeded can you explain me why please ? 

    Wednesday, January 22, 2014 4:28 PM
  • If i am not mistaken, Sender is always a listbox in the selection changed event.

    so

    e.OriginalSource == sender as  TextBox 

    would always yield false, as you compare e.OriginalSource with null.

    I recommend against using selection changed with listbox. If you really need some sort of selection, use a IsSelected property in your binded object and set/unset that with you tap it.

    Selection changed is just too confusing and error prone.

    Wednesday, January 22, 2014 5:04 PM
  • But I  am very curious about why my method did not work and yours succeeded can you explain me why please ?

    First of all, the if expression will be evaluated like this because of operator precedence:

        if (  e.OriginalSource == (sender as TextBox)  )

    sender is your ListBox, so (sender as TextBox) evaluates to null.
    e.OriginalSource is also null, so the if statement evaluates to true.

    Check all of this by setting a debugger breakpoint on the statement and examining the values.

    The meaning of OriginalSource is a bit involved. Look at the documentation: http://msdn.microsoft.com/en-us/library/windowsphone/develop/system.windows.routedeventargs.originalsource(v=vs.105).aspx
    • Edited by mSpot Inc Wednesday, January 22, 2014 5:15 PM
    Wednesday, January 22, 2014 5:10 PM
  • @mcosmin  @mSpot Inc

    Ok , Thank you very much for your explanations,  I will bear that in mind for the future !

    Wednesday, January 22, 2014 10:25 PM