0

listboxファイルのレコードを保存する がありますXML。私の XMLfile には、名前、宛先、従業員 ID などの複数の要素があります。リストボックスには、XMLfile のすべての名前の内容が表示されます。

<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

ListBox を Name などの特定のプロパティにバインドしてから、TextBlock をこのリストボックスにバインドできます。必要に応じて、以下に示すように、プロパティごとに個別の TextBlocks または単一の TextBlocks を使用できます...

<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

リストボックスがいっぱいになった場合は、すでに機能しているはずです。ElementName バインディングにタイプミスがあるだけで、listBox1 である必要があります。

また、名前だけでなくすべての詳細が必要な場合は、コンバーターを作成して、バインド パスに SelectedItem のみを残すことができます。次に、コンバーターは、Textbox に表示される文字列を返す Information インスタンスから詳細をフォーマットするか、いくつかのテキストボックスを使用します。

于 2013-09-10T11:23:49.860 に答える