RKT
RKT

Reputation: 81

Wpf listbox to textbox binding

I want to bind listbox seleceted item to textbox when click i got like this

System.Windows.Controls.ListBoxItem:

how to trim that or if there any alternate way just let me know.

Thanks in Advance

What I have tried:

txtName.Text = List.SelectedValue.ToString();

Upvotes: 0

Views: 1811

Answers (2)

Amine
Amine

Reputation: 1346

You can do it by xaml. you don't need cs code

<ListBox x:Name="MyList"></ListBox>
<TextBox Text="{Binding ElementName=MyList, Path=SelectedItem.Property}"/>

Don't forget to select the property that you want show

Upvotes: 1

Bolu
Bolu

Reputation: 8786

You could override ToString() method in your item's class. Then it will show what ever you put in that method instead of showing its 'Type' which is the default ToString() result.

You could also directly bind to the Property of the item rather than the item itself, so that it will show the correct value (e.g. Item.Name)

Upvotes: 0

Related Questions