Solved

wpf databinding combobox to observable collection

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

Instantly Create Instructional Tutorials

Contextual Guidance at the moment of need helps your employees adopt to new software or processes instantly. Boost knowledge retention and employee engagement step-by-step with one easy solution.

Question has a verified solution.

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

Suggested Solutions

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
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.
How to Install VMware Tools in Red Hat Enterprise Linux 6.4 (RHEL 6.4) Step-by-Step Tutorial

734 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