13
votes

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??

4

4 Answers

22
votes

You have 2 problems with your binding:

  1. You are using the Source property instead of the ElementName to specify the list box name
  2. You are trying to bind to a Content property which (I am assuming) does not exist on your Player object. This happens because the SelectedItem property of the ListBox is an instance of Player when you specify an ItemsSource as you have

To solve this you should change your binding to the following:

<TextBox ... Text="{Binding ElementName=listNames, Path=SelectedItem.GivenName}" ... />
1
votes
<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.

1
votes
        <Page
        x:Class="Studentt1.MainPage"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
           xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
           xmlns:local="using:Studentt1"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
            xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
          mc:Ignorable="d">

             <Grid Background="Wheat">
            <ListBox x:Name="listBox1" ItemsSource="{Binding StudentsList}" 
             SelectedItem="Binding SelectedStud,Mode=TwoWay}"         
             DisplayMemberPath="StudName"    
    HorizontalAlignment="Left" Height="332" Margin="59,173,0,0" VerticalAlignment="Top"                                                                
    <Button Content="Load" Command="{Binding LoadCommand}" HorizontalAlignment="Left" 
    Margin="144,567,0,0" VerticalAlignment="Top"/>

            <Grid  Background="Brown" HorizontalAlignment="Left" Height="352"   
             VerticalAlignment="Top" Width="633">  
             <Grid.RowDefinitions>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="50"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="347"/>
            <ColumnDefinition Width="401"/>
            <ColumnDefinition Width="367*"/>
            <ColumnDefinition Width="251*"/>
        </Grid.ColumnDefinitions>

        <TextBlock Grid.Row="0"  FontSize="30" Grid.Column="0" Text="Registration 
        Number" HorizontalAlignment="Center" Margin="46,0,25,0" Width="276"/>
        <TextBox Grid.Row="0" Grid.Column="1"  Text="{Binding 
        ElementName=listBox1,Path=SelectedItem.RegNo,Mode=TwoWay}"/>
        <TextBlock Grid.Row="1" Grid.Column="0" FontSize="30" Text="Name"  
        HorizontalAlignment="Center" Margin="144,0,124,0" Width="79"/>
        <TextBox Grid.Row="1" Grid.Column="1" Text="{Binding  
        ElementName=listBox1,Path=SelectedItem.StudName,Mode=TwoWay}"/>
        <TextBlock Grid.Row="2" Grid.Column="0" FontSize="30" Text="Age" 
        HorizontalAlignment="Center" Margin="157,0,137,0" Width="53"/>
        <TextBox Grid.Row="2" Grid.Column="1" Text="{Binding 
        ElementName=listBox1,Path=SelectedItem.Age,Mode=TwoWay}"/>
       </Grid>


      </Grid>
      </Page>

here i bind the selected item of list box to text box..

you can find zip file for full source code

0
votes

You should use RelativeSource to access the ListBox, e.g.:

  <TextBox ... Text="{Binding RelativeSource={RelativeSource
                      AncestorType={x:Type ListBox}}, Path=SelectedItem.Content}" .... />