locked
Listview selected Value in WPF RRS feed

  • Question

  • Hi all,

    How to get the values from the current selected row in listview using WPF....???

    Can any one plz help me....??

    Regards,
    Pon Kumar
    Wednesday, June 25, 2008 6:16 AM

Answers

  • If you add a SelectionChanged Event to your ListView then add something akin to this in your code:
     
    private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)  
    {  
       DependencyObject dependancyObj = (DependencyObject)e.OriginalSource;  
       MyObject myObject = getSelectedObject(dependancyObj);  
    }  
     
     
     
    private MyObject getSelectedObject(DependencyObject dependancyObj)  
    {  
       while ((dependancyObj != null) && !(dependancyObj is ListViewItem))  
       {  
          dependancyObj = VisualTreeHelper.GetParent(dependancyObj);  
       }  
       if (dependancyObj == null)  
          return null;  
     
       return (MyObject)listView.ItemContainerGenerator.ItemFromContainer(dependancyObj);  
    }  
     

     

    • Edited by _tom_A Wednesday, June 25, 2008 8:51 AM typo
    • Proposed as answer by Marco Ragogna Wednesday, June 25, 2008 2:06 PM
    • Marked as answer by Marco Zhou Tuesday, July 1, 2008 9:32 AM
    Wednesday, June 25, 2008 8:48 AM

All replies

  • If you add a SelectionChanged Event to your ListView then add something akin to this in your code:
     
    private void listView_SelectionChanged(object sender, SelectionChangedEventArgs e)  
    {  
       DependencyObject dependancyObj = (DependencyObject)e.OriginalSource;  
       MyObject myObject = getSelectedObject(dependancyObj);  
    }  
     
     
     
    private MyObject getSelectedObject(DependencyObject dependancyObj)  
    {  
       while ((dependancyObj != null) && !(dependancyObj is ListViewItem))  
       {  
          dependancyObj = VisualTreeHelper.GetParent(dependancyObj);  
       }  
       if (dependancyObj == null)  
          return null;  
     
       return (MyObject)listView.ItemContainerGenerator.ItemFromContainer(dependancyObj);  
    }  
     

     

    • Edited by _tom_A Wednesday, June 25, 2008 8:51 AM typo
    • Proposed as answer by Marco Ragogna Wednesday, June 25, 2008 2:06 PM
    • Marked as answer by Marco Zhou Tuesday, July 1, 2008 9:32 AM
    Wednesday, June 25, 2008 8:48 AM
  • The answer is too complicated would you please make it simple one:

     

    i'm having a listview which is having name,address when i select a row and that time i need to set that selected value to textbox,

    see for example in listView1_SelectionChanged event

    i write

      txtName.Text = listView1.Items[0].ToString();

    but it is not giving proper output i get "System.Data.DataRowView" so what i need to do??


    Ashish Fugat (9960978134) Software Engineer
    Monday, July 4, 2011 11:55 AM