dcsimg
Results 1 to 7 of 7

Thread: Retrieving the displayed value in a ListBox?

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,880

    Retrieving the displayed value in a ListBox?

    How do you retrieve the displayed value in a ListBox in C#? I know you can use the SelectedValue property to retrieve the the hidden value (typically a key value).

    Thanks,
    Blake

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,733

    Re: Retrieving the displayed value in a ListBox?

    The Text property will give you the String that represents the item in the list.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,880

    Re: Retrieving the displayed value in a ListBox?

    There wasn't a "Text" property to choose from in the intellisense.
    Blake

  4. #4
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,733

    Re: Retrieving the displayed value in a ListBox?

    There is for me, in WinForms at least. Are you talking about Web Forms or WPF?

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,880

    Re: Retrieving the displayed value in a ListBox?

    I'm learning WPF so I was using that...
    Blake

  6. #6
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    102,733

    Re: Retrieving the displayed value in a ListBox?

    Quote Originally Posted by blakemckenna View Post
    I'm learning WPF so I was using that...
    And you thought that it would be a good idea to neither post in the dedicated WPF forum nor mention that it was a WPF project? It wasn't.

    As far as I'm aware, there's no simple way to get the displayed text for the selected item. The SelectedValue property is likely provided because it is important for parent/child relations but the text of the item is really only useful for the eyes of the user. You should know what the bound item type is for your ListBox, cast the SelectedItem as that type and then get the desired property value from that. If you are using MVVM, as any serious WPF developer does, then your question is moot because you'd always be working with a typed property of a view model anyway. While you can write all your code in event handlers as you are used to with WinForms, you shouldn't do so on WPF. Familiarise yourself with the MVVM design pattern ASAP as you're not really developing in WPF without it.

  7. #7

    Thread Starter
    PowerPoster
    Join Date
    Jan 2004
    Location
    Southern California
    Posts
    4,880

    Re: Retrieving the displayed value in a ListBox?

    Thanks for the tip jmc...
    Blake

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Featured


Click Here to Expand Forum to Full Width