【问题标题】:How can I bind a listbox selecteditem content to a textbox?如何将列表框选定项内容绑定到文本框?
【发布时间】:2011-09-21 17:55:06
【问题描述】:

我有一个列表框,当 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 时那样设置文本框?我只需要单向绑定!! 我能做什么??

【问题讨论】:

    标签: wpf linq binding


    【解决方案1】:

    您的绑定有 2 个问题:

    1. 您正在使用 Source 属性而不是 ElementName 来指定列表框名称
    2. 您正在尝试绑定到您的Player 对象上不存在的内容属性(我假设)。发生这种情况是因为当您指定 ItemsSource 时,ListBoxSelectedItem 属性是 Player 的一个实例

    要解决这个问题,您应该将绑定更改为以下内容:

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

    【讨论】:

      【解决方案2】:
      <TextBox ... Text="{Binding ElementName=listNames, Path=SelectedItem.Name}" ... />
      

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

      【讨论】:

        【解决方案3】:
                <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 文件

        【讨论】:

        • 你在说什么 ZIP 文件?
        【解决方案4】:

        您应该使用RelativeSource 访问列表框,例如:

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2011-10-13
          • 1970-01-01
          • 1970-01-01
          • 2011-05-11
          • 2011-03-23
          • 2017-04-17
          • 1970-01-01
          • 2011-02-10
          相关资源
          最近更新 更多