Search code examples
xamlc#-4.0windows-store-apps

How to bind a combo box with a List of string within another List of objects


Windows store App 8.1 : How to bind a combo box with a List of string within another List of objects, is this possible?

Here is what i am trying.

 public class CompanyContact
    {    

            public int ContactId { get; set; }   

            public string Name { get; set; }    

            public List<string> EmailAddress { get; set; }    

    }

 private Company _selectedCompany;
        public CompanyContact SelectedCompany
        {
            get { return _selectedCompany; }
            private set
            {
                _selectedCompany= value;

            }
        }

private IEnumerable<CompanyContact> _companyContact;
 public IEnumerable<CompanyContact> CompanyContacts
        {
            get { return _companyContact; }
            set
            {
                _companyContact= value;
            }
        }

Here is the XAMl that uses it

<ComboBox Grid.Column="0" Grid.Row="0" MinWidth="500" ItemsSource="{Binding CompanyContacts, Mode=TwoWay}" 
                                    PlaceholderText="Select" DisplayMemberPath="EmailAddress" HorizontalAlignment="Left"
                                        SelectedItem="{Binding SelectedCompany, Mode=TwoWay}"/>
  1. How do i display all the emails for all company's contact in the dropdown
  2. Once one email is selected i need the company object that holds the email or the Contactid for the selected email.

What will be the best approach above.


Solution

  • You set DisplayMemberPath="EmailAddress" but your EmailAddress property is a List. I would create a new get only string property like DisplayEmailAddresses that joins the items in the EmailAddress List and use that as DisplayMemberPath.

    This is assuming you want a combobox that has one line for each CompanyContact and displays only their email addresses.