wpf 如何将列表框选定项内容绑定到文本框?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7504361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 23:05:55  来源:igfitidea点击:

How can I bind a listbox selecteditem content to a textbox?

wpflinqbinding

提问by Max

I have a listbox thats getting binded by this query when TextName content changes:

当 TextName 内容更改时,我有一个列表框被此查询绑定:

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:

它显示以文本框上的文本开头的球员姓名。现在,当我单击列表框中的任何项目(名称)时,我需要 TextName 显示在列表框中选择的播放器名称。我试图以这种方式绑定它:

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

但是,当我单击 ListboxItem 时,文本框会被清除并且不显示任何内容。我只需要一种方式绑定!!我能做什么??

回答by Steve Greatrex

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 Playerobject. This happens because the SelectedItemproperty of the ListBoxis an instance of Playerwhen you specify an ItemsSourceas you have
  1. 您正在使用 Source 属性而不是 ElementName 来指定列表框名称
  2. 您正在尝试绑定到您的Player对象上不存在的 Content 属性(我假设)。这是因为SelectedItem财产ListBox是一个实例Player,当你指定ItemsSource你有

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

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

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

回答by Miklós Balogh

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

This binds the TextBox.Textto the ListBoxes - called listNames - SelectedItem, which contains Playerobjects, and you need its Name property.

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

回答by user2652194

        <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

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

回答by Amittai Shapira

You should use RelativeSourceto access the ListBox, e.g.:

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

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