0

I spend few hours on this working on bigger project, so I have made simple example. Problem is when you press "Add" button, it adds numbers to ComboBox item source property...Great, but when you open or select any item from comboBox, binding stops working. I must be missing something.

XAML:

....
<Grid>
    <ComboBox x:Name="comboBox" HorizontalAlignment="Left" Margin="82,63,0,0" 
               VerticalAlignment="Top" Width="120"/>
    <Button x:Name="AddButton" Content="Add" HorizontalAlignment="Left" 
             Margin="82,143,0,0" VerticalAlignment="Top" Width="75" 
             Click="NewNumberClick"/>
</Grid>
...

C# code:

namespace ComboBoxBinding
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
    private List<double> _numbers;
    Binding comboBoxBinding;
    public List<double> Numbers
    {
        get
        {
            return _numbers;
        }
        set
        {
            _numbers = value;
            OnPropertyChanged("Numbers");
        }
    }

    public MainWindow()
    {
        InitializeComponent();
        Numbers = new List<double>(){ 1.0, 2.0, 3.0};

        comboBoxBinding = new Binding();
        comboBoxBinding.Path = new PropertyPath("Numbers");
        comboBoxBinding.Mode = BindingMode.TwoWay;
        BindingOperations.SetBinding(comboBox, ComboBox.ItemsSourceProperty, comboBoxBinding);

        DataContext = this;
    }

    private void NewNumberClick(object sender, RoutedEventArgs e)
    {
        Random rand = new Random(); 
        double newNumber = 2.0 - rand.NextDouble(); 
        Numbers.Add(newNumber);
    }

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged(string propertyName = null)
    {
        if (PropertyChanged != null)
            PropertyChanged.Invoke(this, new 
    PropertyChangedEventArgs(propertyName));
    }
  }
}
sirandy
  • 1,834
  • 5
  • 27
  • 32
Popin
  • 38
  • 4

2 Answers2

1

Use ObservableCollection instead of List. List don't provide a notification when somethings inside a list changes.

ObservableCollection is a collection that allows code outside the collection be aware of when changes to the collection (add, move, remove) occur. It is used heavily in WPF and Silverlight but its use is not limited to there. Code can add event handlers to see when the collection has changed and then react through the event handler to do some additional processing. This may be changing a UI or performing some other operation.

See: What is the use of ObservableCollection in .net?

Kevin Kouketsu
  • 786
  • 6
  • 20
  • It is working, thanks. Still it is interesting that `List` with `OnPropertyChanged` is working only until you unroll the `ComboBox`. – Popin Nov 13 '18 at 07:49
1

your source is a List, it won't notify the UI about member updates. You could use ObservableCollection instead or call OnPropertyChanged each time after you do .Add

More importantly you should use a real DataContext instead of your UI class and you should do the binding in xaml not in code behind

Steve
  • 11,696
  • 7
  • 43
  • 81