您可以将 ListBox 绑定到某个特定属性,例如 Name,然后将 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;
}
}