Solved

wpf databinding combobox to observable collection

Posted on 2013-12-12
4
2,238 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
How do ASP.NET and MVC work together? 4 28
Problem to error 4 45
Help with C#, MVC, razor. 6 23
Wav problem 4 16
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
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…
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, f…

910 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

16 Experts available now in Live!

Get 1:1 Help Now