wpf 将列表中的多个变量绑定到一个组合框

wnavrhmk  于 2023-10-22  发布在  其他
关注(0)|答案(1)|浏览(142)

我刚开始学习WPF和C#。
考虑:

private void TransferAccountButtonClick(object sender, RoutedEventArgs e)
{
    List<Client> allClients = Client.JsonToList();
    TransferStackPanel.Visibility = Visibility.Visible;

    TransferNameCombobox.DataContext = allClients;
    TransferNameCombobox.DisplayMemberPath = "surname";
}

我需要在一个组合框中显示多个字段。事

TransferNameCombobox.DisplayMemberPath = "surname" + " " + "name" + " " + "patronymic";

如果我这样做,它将显示空字段。我明白“姓”不是一个字符串,但我不明白如何做到这一点。
在XAML中,我只有:

<ComboBox x:Name="TransferNameCombobox" ItemsSource="{Binding}"/>
jhdbpxl9

jhdbpxl91#

TransferNameCombobox.DataContext = allClients;改为TransferNameCombobox.ItemsSource = allClients;并删除TransferNameCombobox.DisplayMemberPath = "surname";
然后在xaml中,使用这个MultiBinding结构:

<ComboBox x:Name="TransferNameCombobox">
            <ComboBox.ItemTemplate>
                <DataTemplate>
                    <TextBlock>
                        <TextBlock.Text>
                            <MultiBinding StringFormat="{}{0}, {1}, {2}">
                                <Binding Path="surname"/>
                                <Binding Path="name"/>
                                <Binding Path="patronymic"/>
                            </MultiBinding>
                        </TextBlock.Text>
                    </TextBlock>
                </DataTemplate>
            </ComboBox.ItemTemplate>
        </ComboBox>

相关问题