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时,文本框才会被清除,并且不会显示任何内容..在设置DisplayMemeberPath时,我可能需要设置文本框,就像我对列表框一样吗?我只需要单向绑定!!我能做什么??
4 个解决方案
#1
18
You have 2 problems with your binding:
绑定有2个问题:
- You are using the Source property instead of the ElementName to specify the list box name
- 您正在使用Source属性而不是ElementName来指定列表框名称
- You are trying to bind to a Content property which (I am assuming) does not exist on your
Player
object. This happens because theSelectedItem
property of theListBox
is an instance ofPlayer
when you specify anItemsSource
as you have - 您正在尝试绑定到您的Player对象上不存在(我假设)的Content属性。发生这种情况是因为当您指定ItemsSource时,ListBox的SelectedItem属性是Player的实例
To solve this you should change your binding to the following:
要解决此问题,您应该将绑定更改为以下内容:
<TextBox ... Text="{Binding ElementName=listNames, Path=SelectedItem.GivenName}" ... />
#2
1
<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.
这将TextBox.Text绑定到ListBoxes - 名为listNames - SelectedItem,它包含Player对象,您需要其Name属性。
#3
0
You should use RelativeSource
to access the ListBox, e.g.:
您应该使用RelativeSource来访问ListBox,例如:
<TextBox ... Text="{Binding RelativeSource={RelativeSource
AncestorType={x:Type ListBox}}, Path=SelectedItem.Content}" .... />
#4
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>
here i bind the selected item of list box to text box..
在这里我将列表框的选定项绑定到文本框..
you can find zip file for full source code
你可以找到完整源代码的zip文件
#1
18
You have 2 problems with your binding:
绑定有2个问题:
- You are using the Source property instead of the ElementName to specify the list box name
- 您正在使用Source属性而不是ElementName来指定列表框名称
- You are trying to bind to a Content property which (I am assuming) does not exist on your
Player
object. This happens because theSelectedItem
property of theListBox
is an instance ofPlayer
when you specify anItemsSource
as you have - 您正在尝试绑定到您的Player对象上不存在(我假设)的Content属性。发生这种情况是因为当您指定ItemsSource时,ListBox的SelectedItem属性是Player的实例
To solve this you should change your binding to the following:
要解决此问题,您应该将绑定更改为以下内容:
<TextBox ... Text="{Binding ElementName=listNames, Path=SelectedItem.GivenName}" ... />
#2
1
<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.
这将TextBox.Text绑定到ListBoxes - 名为listNames - SelectedItem,它包含Player对象,您需要其Name属性。
#3
0
You should use RelativeSource
to access the ListBox, e.g.:
您应该使用RelativeSource来访问ListBox,例如:
<TextBox ... Text="{Binding RelativeSource={RelativeSource
AncestorType={x:Type ListBox}}, Path=SelectedItem.Content}" .... />
#4
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>
here i bind the selected item of list box to text box..
在这里我将列表框的选定项绑定到文本框..
you can find zip file for full source code
你可以找到完整源代码的zip文件