user1786794
user1786794

Reputation:

Populate a WPF Combobox from Service

I am trying to populate my combobox from a service response. The service returns an array of object like following

MyService.FirmSocial[] firmSocialList = client.GetActiveSocialMediaTypes();

I have checked, the firmSocialList populates properly. I need to populate my combobox with these values.

I have tried this in my code behind

cbSocialMediaTypes.ItemsSource = firmSocialList;
cbSocialMediaTypes.DisplayMemberPath = "socialMediaValue";
cbSocialMediaTypes.SelectedValuePath = "socialMediaType";

I also tried the same thing on the XAML side, but all I am getting is bunch of empty strings in my combobox. The thing is though, the number of elements matches with the item count of the combobox (of empty strings).

And yes, the property names of the FirmSocial object is correct.

The FirmSocial class

public class FirmSocial
{
    private int socialMediaType;
    private string socialMediaValue;
    public int SocialMediaType
    {
        get
        {
            return socialMediaType;
        }
        set
        {
            socialMediaType = value;
        }
    }
    public string SocialMediaValue
    {
        get
        {
            return socialMediaValue;
        }
        set
        {
            socialMediaValue = value;
        }
    }
}

And I have also tried this in my XAML section;

<ComboBox x:Name="cbSocialMediaTypes" HorizontalAlignment="Left" Margin="56,8,0,0" VerticalAlignment="Top" Width="211"
ItemsSource="{Binding firmSocialList}"
DisplayMemberPath="socialMediaType"
SelectedValuePath="socialMediaType" />

Thanks.

Upvotes: 0

Views: 407

Answers (2)

Valentin
Valentin

Reputation: 5488

The problem is that you set the DisplayMemberPath and the SelectedValuePath to a private fields instead of a public properties.

cbSocialMediaTypes.DisplayMemberPath = "socialMediaValue";
cbSocialMediaTypes.SelectedValuePath = "socialMediaType";

Change it to

cbSocialMediaTypes.DisplayMemberPath = "SocialMediaValue";
cbSocialMediaTypes.SelectedValuePath = "SocialMediaType";

Upvotes: 0

Joe
Joe

Reputation: 7004

DisplayMemberPath is case sensitive.

DisplayMemberPath="socialMediaType"

is saying trying to bind to your private field, not your public property. Try:

DisplayMemberPath="SocialMediaType"

Upvotes: 2

Related Questions