We help IT Professionals succeed at work.
Get Started

Binding List<string> to Combobox

Xrth
Xrth asked
on
8,705 Views
Last Modified: 2013-11-12
I want to bind a List to a combobox. But somehow its not working.
If i assigne the data to the combobox its showing up, but if i change the datasource later, its not updating. So basically the binding is ot working.
I dont know if it is importent or not but the class is a singleton.
// Here is the Class
 
public class Items : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    static Items instance = null;
    static readonly object padlock = new object();
 
    Items()
    {
    }
 
    public static Items Instance
    {
        get
        {
            lock (padlock)
            {
                if (instance == null)
                {
                    instance = new Items();
                }
                return instance;
            }
        }
    }
 
    public void NotifyPropertyChanged(string propertyName)
    {
        if (PropertyChanged != null)
        {
            PropertyChanged(this,
                new PropertyChangedEventArgs(propertyName));
        }
 
    }
 
    public List<string> _ItemList = new List<string>();
    public List<string> ItemList 
    {            
        get
        {
            return _ItemList;                
        }
 
        set
        {
            _ItemList = value;
            NotifyPropertyChanged("items");
        }
    }
}
 
//Here is the xaml code of the combobox
<ComboBox ItemsSource="{Binding  ItemList, Mode=OneWay}" 
 
// This is a function of the c# file from the UC where i want to bind the data
 
        private void btnLoad_Click(object sender, RoutedEventArgs e)
        {
            if (items.ItemList.Count <= 0)
            {
                con.sendData("LoadItems");
            }
            ddItems.DataContext = Classes.Singleton.Items.Instance;
        }

Open in new window

Comment
Watch Question
Commented:
This problem has been solved!
Unlock 1 Answer and 3 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE