如何将列表框selecteditem内容绑定到文本框?

时间:2011-09-21 17:55:07

标签: wpf linq binding

我有一个列表框,当TextName内容发生变化时,会被此查询绑定:

var players =
    from p in context.Player
    where p.GivenName.StartsWith(TextName.Text.Trim())
    select p;

listNames.ItemsSource = players.ToList();

它显示以文本框中的文本开头的玩家名称。现在,当我从列表框中单击任何项​​目(名称)时,我需要TextName显示在列表框中选择的玩家名称。我试着用这种方式绑定它:

<TextBox ... Text="{Binding Source=listNames, Path=SelectedItem.Content}" ... />

但是当我单击一个ListboxItem时,文本框才会被清除并且不会显示任何内容..在设置DisplayMemeberPath时,我是否必须像设置列表框一样设置文本框?我只需要单向绑定!! 我该怎么办?

4 个答案:

答案 0 :(得分:19)

绑定有两个问题:

  1. 您正在使用Source属性而不是ElementName来指定列表框名称
  2. 您正在尝试绑定到您的Player对象上不存在(我假设)的Content属性。发生这种情况是因为当您指定SelectedItem时,ListBox的{​​{1}}属性是Player的实例
  3. 要解决此问题,您应该将绑定更改为以下内容:

    ItemsSource

答案 1 :(得分:1)

<TextBox ... Text="{Binding ElementName=listNames, Path=SelectedItem.Name}" ... />

这会将TextBox.Text绑定到ListBoxes - 名为listNames - SelectedItem,其中包含Player个对象,您需要其Name属性。

答案 2 :(得分:0)

您应该使用RelativeSource来访问ListBox,例如:

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

答案 3 :(得分:0)

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

这里我将列表框的选定项目绑定到文本框..

您可以找到完整源代码的zip文件

相关问题