Solved

wpf databinding combobox to observable collection

Posted on 2013-12-12
4
2,403 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 23

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
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.
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

728 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