0

我有一个listbox存储文件记录的XML文件。我的 XMLfile 有多个元素,如姓名、目的地、员工 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}"/>
4

3 回答 3

0

如果列表框填满了它应该已经可以工作了,你只是在 ElementName 绑定中有一个错字,它应该是 listBox1。

此外,如果您想要所有详细信息而不仅仅是名称,您可以制作一个转换器并在绑定路径中只保留 SelectedItem。然后,转换器将格式化信息实例的详细信息,返回要在 texbox 中显示的字符串,或者只使用几个文本框。

于 2013-09-10T11:23:49.860 回答
0

您可以将 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;
    }
}
于 2013-09-10T12:08:28.880 回答
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>
于 2013-09-10T11:29:09.147 回答