Popin
Popin

Reputation: 38

ComboBox ItemSourceProperty losts binding when item selected

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));
    }
  }
}

Upvotes: 0

Views: 49

Answers (2)

Kevin Kouketsu
Kevin Kouketsu

Reputation: 826

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?

Upvotes: 1

Steve
Steve

Reputation: 11963

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

Upvotes: 1

Related Questions