0
votes

I have a problem of performance with an observable collection. This foreach is binded with a combobox (MaAcquisition is an observable collection) :

 for (double i = 0.1; i <= 5; i += 0.1)
 {
     MaAcquisition.Add($"{i:0.0}");
     x++;
 }

With this foreach, every loop, I bind value to combobox, it's very slow.

So to improve this I do this :

List<String> MaAcquisitionList = new List<String>();
for (double i = 0.1; i <= 5; i += 0.1)
{
    MaAcquisitionList.Add($"{i:0.0}");
    x++;
}
MaAcquisition = MaAcquisitionList;

It's working but after this "foreach" I do a binding to choose the "selectedItem" =>

SelectedMa = MaAcquisition[x - 1];

Selected item is binded to my combobox and it's not working (the selected item is blank).

    <ComboBox ItemsSource="{Binding MaAcquisition, Mode=TwoWay}" SelectedItem="{Binding SelectedMa, Mode=TwoWay}"  IsEnabled="{Binding PreheatingDisable}"/>

And finally, there is the code for "SelectedMa" :

 public string SelectedMa
        {
            get { return _selectedMa; }
            set
            {
                _selectedMa= value;
                OnPropertyChanged();
                RaisePropertyChanged();     
        }

Do you have an idea for this problem?

Thank's.

1
Can you show the code for the SelectedMa property/field?slugster
You could take a look over here: stackoverflow.com/questions/8606994/…Jeroen van Langen
SelectedMa should notify of changes. See for INotifyPropertyChanged examples.AnjumSKhan

1 Answers

2
votes

About your ObservableCollection being slow:

Every time you add an item to ObservableCollection, the event CollectionChanged is rised that informs the views (in this case, this is a ListCollectionView that is generated when you bind to your MaAcquisition, that is in turn bound to your ComboBox) bound to this ObservableCollection that its Items has changed. (More details about that here)

To prevent this event being rised for every item when you add/replace a batch of elements, you'll have to, for example, extend the default ObservableCollection class. Here it is explained how to do this.

About your SelectedMa not changing, are you sure you implemented INotifyPropertyChanged for the class that contains this property and that you are rising it properly?