Home > Mobile >  bind multiple variables from list to one combobox
bind multiple variables from list to one combobox

Time:08-04

I Just start learn WPF and 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

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

CodePudding user response:

Instead of TransferNameCombobox.DataContext = allClients; make it TransferNameCombobox.ItemsSource = allClients; and delete TransferNameCombobox.DisplayMemberPath = "surname";

And then in the xaml, use this MultiBinding structure:

        <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>
  • Related