wpf databinding combobox to observable collection

Posted on 2013-12-12
Medium Priority
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; }
                _tAccountName = value;

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

Open in new window

then I construct my MainWindow like so

    public partial class MainWindow : Window
        public ObservableCollection<AccountNameClass> accountNameObCollection {get; set;}
        public MainWindow()
            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

                    ItemsSource="{Binding accountNameObCollection}"
                    DisplayMemberPath="{Binding tAccountName}">

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

Question by:solarissf
  • 2
LVL 13

Expert Comment

ID: 39714387
I would try to add:
in your ItemsSource binding.

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

Open in new window


Author Comment

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

Accepted Solution

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


Author Comment

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

Featured Post

The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

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!
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…
To export Lotus Notes to Outlook PST or Exchange and Domino Server files to Exchange Server or PST files with ease, go for Kernel for Lotus Notes to Outlook conversion tool. Through the video, you can watch the conversion process. A common user with…

597 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