我有listbox
存储XML
文件的记录。我的XMLfile有多个元素,如Name,Destination,employee ID。列表框将显示XML文件的所有名称内容。
<Information>
<Name>Goutham</Name>
<Destination>dar</Destination>
<EmployeeID>da</EmployeeID>
</Information>
<Information>
<Name>Adam</Name>
<Destination>ads</Destination>
<EmployeeID>dwa</EmployeeID>
</Information>
<Information>
<Name>dwad</Name>
<Destination>wadwa</Destination>
<EmployeeID>daw</EmployeeID>
</Information>
列表框显示所有不同的名称,如Goutham,Adam,dwad。 现在如果我在列表框中选择Goutham,我需要在文本框中显示Goutham的所有细节。我该怎么做?
这是我的xaml文件
<ListBox Height="251" HorizontalAlignment="Left" Margin="330,23,0,0" Name="listBox1" VerticalAlignment="Top" Width="170" IsSynchronizedWithCurrentItem="True" DisplayMemberPath="Name" ItemsSource="{Binding}"/>
<TextBox Height="23" HorizontalAlignment="Left" Margin="141,42,0,0" Name="textBox1" VerticalAlignment="Top" Width="173" Text="{Binding ElementName= listbox1, Path=SelectedItem.Name}"/>
答案 0 :(得分:0)
如果列表框填满它应该已经全部工作,你只需要在ElementName绑定中输入拼写,它应该是listBox1。
此外,如果您想要所有细节而不仅仅是名称,您可以创建转换器并仅在绑定路径中保留SelectedItem。然后,转换器将格式化信息实例的详细信息,返回要在texbox中显示的字符串,或者只使用多个文本框。
答案 1 :(得分:0)
另一种选择是使用StringFormat进行MultiBinding:
<TextBlock>
<TextBlock.Text>
<MultiBinding StringFormat="{0} {1} {2}">
<Binding ElementName="listbox1", Path="SelectedItem.Name" />
<Binding ElementName="listbox1", Path="SelectedItem.Destination" />
<Binding ElementName="listbox1", Path="SelectedItem.EmployeeID" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
答案 2 :(得分:0)
您可以将ListBox绑定到一次特定属性,例如Name&amp;然后将TextBlock绑定到此列表框。根据您的需要,您可以为每个属性或单个TextBlock创建单独的TextBlock ...如下所示...
<Window x:Class="ListBoxDataBinding.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:c="clr-namespace:ListBoxDataBinding"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<c:EmployeeConverter x:Key="myEmployeeConverter"/>
</Window.Resources>
<StackPanel Orientation="Vertical">
<ListBox x:Name="lbEmployee">
<ListBox.ItemTemplate>
<DataTemplate>
<Label Content="{Binding Name}" />
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
<Grid>
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<TextBlock Grid.Row="0" Grid.Column="0" Width="248" Height="24" Text="Employee Name" />
<TextBlock Grid.Row="0" Grid.Column="1" Width="248" Height="24" Text="{Binding ElementName=lbEmployee, Path=SelectedItem.Name}" />
<TextBlock Grid.Row="1" Grid.Column="0" Width="248" Height="24" Text="Employee EmployeeId" />
<TextBlock Grid.Row="1" Grid.Column="1" Width="248" Height="24" Text="{Binding ElementName=lbEmployee, Path=SelectedItem.EmployeeId}" />
<TextBlock Grid.Row="2" Grid.Column="0" Width="248" Height="24" Text="Employee Destination" />
<TextBlock Grid.Row="2" Grid.Column="2" Width="248" Height="24" Text="{Binding ElementName=lbEmployee, Path=SelectedItem.Destination}" />
<TextBlock Grid.Row="3" Grid.Column="0" Width="248" Height="24" Text="Employee Details" />
<TextBlock Grid.Row="3" Grid.Column="2" Width="248" Height="24">
<TextBlock.Text>
<MultiBinding Converter="{StaticResource myEmployeeConverter}" ConverterParameter="FormatEmployee">
<Binding ElementName="lbEmployee" Path="SelectedItem.Name" />
<Binding ElementName="lbEmployee" Path="SelectedItem.EmployeeId" />
<Binding ElementName="lbEmployee" Path="SelectedItem.Destination" />
</MultiBinding>
</TextBlock.Text>
</TextBlock>
</Grid>
</StackPanel>
代码背后......
public partial class MainWindow : Window
{
private List<Employee> _lstEmployees;
public MainWindow()
{
InitializeComponent();
Loaded += new RoutedEventHandler(MainWindow_Loaded);
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
_lstEmployees = new List<Employee>();
_lstEmployees.Add(new Employee { Name = "Goutham", EmployeeId = "da", Destination = "dar" });
_lstEmployees.Add(new Employee { Name = "Adam", EmployeeId = "dwa", Destination = "ads" });
_lstEmployees.Add(new Employee { Name = "dwad", EmployeeId = "daw", Destination = "wadwa" });
lbEmployee.ItemsSource = _lstEmployees;
}
}
public class EmployeeConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
string name;
switch ((string)parameter)
{
case "FormatEmployee":
if (values[0] is String)
{
name = values[0] + ", " + values[1] + ", " + values[2];
}
else
{
name = String.Empty;
}
break;
default:
name = String.Empty;
break;
}
return name;
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
string[] splitValues = ((string)value).Split(' ');
return splitValues;
}
}