简体   繁体   English

将列表中的多个变量绑定到一个 combobox

[英]bind multiple variables from list to one combobox

I Just start learn WPF and c#我刚开始学习 WPF 和 c#

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

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

I need to display multiple fields in combox.我需要在组合框中显示多个字段。 Something like就像是

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

If I do this it will show empty fields.如果我这样做,它将显示空字段。 I understand that "surname" is not a string, but I don't understand how to do it我知道“姓”不是字符串,但我不明白该怎么做

in xaml I only have在 xaml 我只有

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

Instead of TransferNameCombobox.DataContext = allClients;而不是TransferNameCombobox.DataContext = allClients; make it TransferNameCombobox.ItemsSource = allClients;让它TransferNameCombobox.ItemsSource = allClients; and delete TransferNameCombobox.DisplayMemberPath = "surname";并删除TransferNameCombobox.DisplayMemberPath = "surname";

And then in the xaml, use this MultiBinding structure:然后在 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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM