question

MojtabaHakim-8125 avatar image
0 Votes"
MojtabaHakim-8125 asked HuiLiu-MSFT commented

Why DataGridComboBoxColumn's ItemsSource will refresh after DataGrid reload ?

I have C# WPF project that is save data from DataGrid into a Table in SQL Server Database

In Database I have two tables as Factor and Commodity that related to each other and

-The ItemsSource of the DatGrid is filled from the Factor table
-The ItemsSource of the DataGridComboBoxColumn is filled with Commoditiy (the Item source of DataGrid and comboBoxColumn are diffrent)

When I want to Realod DataGrid's Data by "select * from Factor" I Only Reloaded the DataGrid's ItemsSource not The ItemsSource of the DataGridComboBoxColumn
why DataGridComboBoxColumn's ItemsSource will refresh (as if moving on each item)

Datagrid-Refresh-1.gif

full code:how-can-i-prevent-the-observablecollection-from-being-refreshed-for-no-reason-in


dotnet-csharpwindows-wpfdotnet-wpf-xaml
· 4
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

is there any idea ?

0 Votes 0 ·
HuiLiu-MSFT avatar image HuiLiu-MSFT MojtabaHakim-8125 ·

Hi,@MojtabaHakim-8125. I see there is an answer in the link. Is your problem solved?

0 Votes 0 ·

some how but , not in Observble Collection ! , is there any way to disable get;set for temporary ?

0 Votes 0 ·
Show more comments

1 Answer

HuiLiu-MSFT avatar image
1 Vote"
HuiLiu-MSFT answered HuiLiu-MSFT edited

A very quick and easy way is to subclass ObservableCollection and suspend notifications when AddRange is called. You could try to refer to it according to your needs.

  public class MyObservableCollection<T> : ObservableCollection<T>
     {
         private bool myNotification = false;
    
         protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
         {
             if (!myNotification)
                 base.OnCollectionChanged(e);
         }
    
         public void AddRange(IEnumerable<T> list)
         {
             if (list == null)
                 throw new ArgumentNullException("list");
    
             myNotification = true;
    
             foreach (T item in list)
             {
                 Add(item);
             }
             myNotification = false;
             OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
         }
     }



If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.