I have a listbox thats getting binded by this query when TextName content changes:
var players =
from p in context.Player
where p.GivenName.StartsWith(TextName.Text.Trim())
select p;
listNames.ItemsSource = players.ToList();
It shows the players names that starts with the text on the textbox. Now when I click any Item (name) from the listbox I need that the TextName shows the player name that's selected on the listbox. I tried to bind it this way:
<TextBox ... Text="{Binding Source=listNames, Path=SelectedItem.Content}" ... />
But when I click a ListboxItem, the textbox just get cleared and does not show anything.. may I have to set up the textbox like I do with the listbox when setting the DisplayMemeberPath???? I need a only one way binding!! What can I do??
You have 2 problems with your binding:
Player
object. This happens because the SelectedItem
property of the ListBox
is an instance of Player
when you specify an ItemsSource
as you haveTo solve this you should change your binding to the following:
<TextBox ... Text="{Binding ElementName=listNames, Path=SelectedItem.GivenName}" ... />
<TextBox ... Text="{Binding ElementName=listNames, Path=SelectedItem.Name}" ... />
This binds the TextBox.Text
to the ListBoxes - called listNames - SelectedItem, which contains Player
objects, and you need its Name property.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With