1

I'm having an issue when I select an item from a DataGridComboBoxColumn. The cell wont display the name of the item I've chosen after I move focus to the next cell. This is the code I have:

DataGridComboBoxColumn cb1 = new DataGridComboBoxColumn();
cb1.ItemsSource = listOStrings;
cb1.TextBinding = new Binding("listOfStrings");
e.Column = cb1;
e.Column.Header = "SomeTitle";

Where listOfStrings is a list that is being updated by the user. I have another DataGridComboBoxColumn that has its ItemSource set to a list of strings that isn't being updated. That one displays the text just fine, although the code for the two is the same. I'm wondering why my cb1 combo box wont display the values after leaving the cell but the other one does?

user2748943
  • 533
  • 2
  • 9
  • 20

2 Answers2

1

I've never done binding the way you are doing it - have you considered moving the UI to the XAML and databinding to a ViewModel? Here is an awesome step by step example on databinding comboboxes. You would just have the combobox be a column within the DataGrid also - similar to this.

Community
  • 1
  • 1
Panh
  • 225
  • 1
  • 9
  • I haven't used XAML as much as I'd like because I'm somewhat new with it. Although the step by step guide you posted helped me with another problem I was having, which is a huge help. Thank you for your answer. – user2748943 Jun 07 '15 at 15:04
  • No problem - glad I could help! Also, if you want to get more into the XAML, I really like [these](https://www.youtube.com/watch?v=rzkStHX4LDM) videos. Start at 1 and work your way up. Good luck! – Panh Jun 08 '15 at 15:56
1

When a binding in WPF is hooked up to a non-static source, the underlying source needs to implement iNotifyPropertyChanged. In your case you may want to use an ObservableCollection as answered here: Why does a string INotifyPropertyChanged property update but not a List<string>?

In your case, it would look something like:

private ObservableCollection<string> _listOStrings = new ObservableCollection<string>();
public ObservableCollection<string> ListOStrings 
{
    get
    {
        return _listOStrings;
    }

    set
    {
        _listOStrings = value;
        OnPropertyChanged("ListOStrings");
    }
}

For more information on iNotifyPropertyChanged from MSDN, see: See: https://msdn.microsoft.com/en-us/library/ms743695(v=vs.110).aspx

Community
  • 1
  • 1
Bobby
  • 467
  • 4
  • 13
  • Thank you for your answer, after reading through Microsoft's link you posted I was able to make it work like a charm. – user2748943 Jun 07 '15 at 15:02