Solved

wpf databinding combobox to observable collection

Posted on 2013-12-12
4
2,206 Views
Last Modified: 2013-12-12
I am attempting to databind an observable collection to a combobox and can't seem to get it right.

First off, here is the class I created to contain the tAccountName property.
namespace CP_v1
{
    public class AccountNameClass : INotifyPropertyChanged
    {
        public event PropertyChangedEventHandler PropertyChanged;
        private string _tAccountName;

        public string tAccountName
        {
            get { return _tAccountName; }
            set
            {
                _tAccountName = value;
                RaisePropertyChanged();
            }
        }

        #region PropertyChanged
        private void RaisePropertyChanged([CallerMemberName] string caller = "")
        {
            if (PropertyChanged != null)
            {
                PropertyChanged(this, new PropertyChangedEventArgs(caller));
            }
        }
        #endregion
    }
}

Open in new window



then I construct my MainWindow like so

    public partial class MainWindow : Window
    {
        public ObservableCollection<AccountNameClass> accountNameObCollection {get; set;}
        public MainWindow()
        {
            InitializeComponent();
            accountNameObCollection = new ObservableCollection<AccountNameClass>();
            comboboxClients.DataContext = this;
}
        private void InitiateButton_Click(object sender, RoutedEventArgs e)
        {
                        accountNameObCollection.Add(new AccountNameClass() {tAccountName = "Stephen"});
                        accountNameObCollection.Add(new AccountNameClass() {tAccountName = "bobby"});
}
}

Open in new window



then the xaml is

                <ComboBox
                    x:Name="comboboxClients"
                    Height="20"
                    Margin="0,0,10,0"
                    Width="100"
                    ItemsSource="{Binding accountNameObCollection}"
                    DisplayMemberPath="{Binding tAccountName}">
                </ComboBox>

Open in new window


When I click the InitiateButtonClick
the combobox is populated with "AccountNameClass", in every item.. rather than the underlying property.

Can someone tell me what I am doing wrong?  I'm sure I did something stupid

thanks!!!
0
Comment
Question by:solarissf
  • 2
4 Comments
 
LVL 13

Expert Comment

by:jonnidip
ID: 39714387
I would try to add:
UpdateSourceTrigger=PropertyChanged
in your ItemsSource binding.

ItemsSource="{Binding accountNameObCollection, UpdateSourceTrigger=PropertyChanged}"

Open in new window

0
 

Author Comment

by:solarissf
ID: 39714400
added UpdateSourceTrigger=PropertyChanged, still no change..
just shows the name of class... not the underlying property
0
 
LVL 22

Accepted Solution

by:
Snarf0001 earned 500 total points
ID: 39714449
Take the {Binding} off of DisplayMemberPath.  It expects just a string, and will grab that property from the bound object.

DisplayMemberPath="tAccountName"
0
 

Author Comment

by:solarissf
ID: 39714494
that worked... thanks!!!!
I knew it was something simple
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now