WPF [SOLVED]: ListView get second column value

WPF [SOLVED]: ListView get second column value

Home Forums Frameworks WPF WPF [SOLVED]: ListView get second column value

Tagged: , ,

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #246876

    Cloudy Point
    Keymaster

    QuestionQuestion

    I am trying to retrieve the second column value of a ListView:

    void PrintText(object sender, SelectionChangedEventArgs args)
    {
        object item = listBox1.SelectedItem;
    
        if (item == null) {
        txtSelectedItem.Text = "No item currently selected.";
    } else {
        txtSelectedItem.Text = item.ToString();
    }
    

    The above will return both columns as one string, however I only want the second column.

    Here is my XAML:

      <ListView x:Name="lvSurname" ItemsSource="{Binding User}"        
          SelectionChanged="lvSurname_SelectionChanged">
        <ListView.View>
          <GridView>
             GridViewColumn Header="Surname" DisplayMemberBinding="{Binding Path=Surname}" />
             GridViewColumn Header="ClientID" DisplayMemberBinding="{Binding Path=ClientID}" />
          </GridView>
      </ListView.View>
    </ListView>
    

    And my User class:

    public class User
    {
        public string Surname { get; set; }
        public string ClientID { get; set; }
    }
    

    How can I do that?

    #246877

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    Is this what you need?

    txtSelectedItem.Text = ((User)listBox1.SelectedItem).ClientID;
    

    Source: https://stackoverflow.com/questions/47955809/listview-get-second-column-value
    Author: Chris Mack
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.