howamith
howamith

Reputation: 201

Trouble binding ComboBox to ObservableCollection<>

I'm having some trouble binding an ObserveableCollection to a combo box. To give some perspective I have a class like so:

class LogOnUser
{
    #region Members
    string _UserName;
    string _Password;
    #endregion

    #region Construction
    public LogOnUser(string username)
    {
        _UserName = username;
    }

    public LogOnUser(string username, string password)
    {
        _UserName = username;
        _Password = password;
    }
    #endregion

    #region Properties
    public string Username
    {
        get { return _UserName; }
        set { _UserName = value; }
    }

    public string Password
    {
        get { return _Password; }
        set { _Password = value; }
    }
    #endregion
}

And then I have another class that uses LogOnUser in an ObserveableCollection:

class LogOnUserCollection: ObservableCollection<LogOnUser>
{
    public LogOnUserCollection() : base()
    {
        Add(new LogOnUser("User1", "password"));
        Add(new LogOnUser("User2", "password"));
    }
}

I have an instance of LogOnUserCollection in a ViewModel that I've named LogOnUsers, and I'm binding this to the ItemsSource of a combo box in my View. However I just want to display the Username property of my collection, so it would have "User1" and "User2" as it's items, but what is actually happening is my ComboBox is just displaying "LogOnUsers.LogOnUser" as its two items. Can someone tell me what I'm doing wrong?

Update Binding xaml code

<UserControl x:Class="MVVM.View"
         xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
         xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
         xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
         xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
         xmlns:local="clr-namespace:MVVM"
         mc:Ignorable="d">

    <UserControl.DataContext>
        <local:PasswordEntryViewModel/>
    </UserControl.DataContext>

    <UserControl.Resources>
        <local:LogOnUserCollection x:Key="LogOnUserCollection"/>
    </UserControl.Resources>

    <Grid>
        <ComboBox ItemsSource="{Binding LogOnUserCollection}"/>
    </Grid>
</UserControl>

Upvotes: 1

Views: 65

Answers (2)

Muds
Muds

Reputation: 4116

You need to set a few more properties than just ItemsSource

A typical combobox usually looks like --

<ComboBox ItemsSource="{Binding ITEMS}"
              DisplayMemberPath="PROPERTY_NAME"
              SelectedItem="{Binding SELECTED_ITEM}"/>

Display member would tell which property should be used to display text in combobox.

And selected item as name suggests will give you the selected item so that you can use it. (though selected item is not directly related to your question but in most cases you will be using it)

Upvotes: 1

mark_h
mark_h

Reputation: 5467

Try setting the display member path as shown below;

<ComboBox ItemsSource="{Binding LogOnUserCollection}"
      DisplayMemberPath="Username"/>

Upvotes: 1

Related Questions