Skip to content

Wpf listbox select item problem

mark dragan

Asked Aug 22, 2009, 6:52 AM EDT5 answers
Share
LinkedInX
Hi everyone I'm new at wpf and I got some easy question:How can I get select item value from listbox? I wrote this code:
label1.Content = listBox1.SelectedItem.ToString();
and I get:System.Cindows.Control.ListBoxItem:Test1;
how can I get just Test1.
Thanks in advance!!

4 Answers

Know the answer? Post it. Somebody with the same question will find it here.

  • 0
    Can you please post the details, how did you solve the problem?
    0
  • 0
    ListBoxItem item=listbox1.SelectedItem;
    label1.Content=item.ToString();
    Hope this helps
    0
  • 0
    or to select the text from an item in listbox :
     Label x = listBox.SelectedItem as Label;
                    String y= sourcelbi.Content.ToString();
    0
  • 0
    Hi Mark,

    You might want to look up Element Binding which allows you to achieve this with no code behind, something along these lines: -
    HTML / XML
    <ListBox x:Name="ListBox1" ItemsSource="{Binding} SelectedValuePath="IDOrSomething" DisplayMemberPath="PropertyYouWantToDisplay" /> 
    <Label Content="{Binding ElementName=ListBox1, Path=SelectedValue}" />
    Cheers
    0
  • 1 short reply
    • 0
      PROBLEM SOLVED
      0