简体   繁体   中英

Async modification of ObservableCollection<T> not represented in DataGrid

It's been a while since I've worked with Forms/WPF. So excuse me if this is a stupid question.

I have an ObservableCollection<myClass> named myObservable where myClass consists of the following:

public class myClass
{
    public string myStringOne { get; set; }
    public string myStringTwo { get; set; }
    public string myStringThree { get; set; }
    public string mystringFour { get; set; }
}

And in the UI.xaml the following control.

<Grid HorizontalAlignment="Left" Height="239" VerticalAlignment="Top" Width="439">
    <DataGrid x:Name="PhoneGrid" VerticalAlignment="Top" Height="134"
              CanUserReorderColumns="False" 
              CanUserResizeColumns="False" 
              CanUserResizeRows="False" 
              CanUserSortColumns="False" 
              Focusable="False" 
              IsReadOnly="True"
        DataContext="{Binding RelativeSource={RelativeSource AncestorType=Window}}"
        ItemsSource="{Binding myObservable}" MouseLeftButtonUp="PhoneGrid_MouseLeftButtonUp" SelectionUnit="Cell" Margin="10,100,10,0" />

The way I set my backgroundworker:

private readonly BackgroundWorker _worker = new BackgroundWorker();
private readonly object _lock = new object();
_worker.DoWork += LoadLines;
var timer = new Timer(Settings.Default.Interval);
timer.Elapsed += timer_Elapsed;
timer.Start();

LoadLines being the function I do my UI blocking things in.

I have attempted to implement things like

// Prepare obervable collection
BindingOperations.EnableCollectionSynchronization(myObservable, _lock);

Aswell as

Dispatcher.Invoke(() => {
    myObservable[index].myString = "anyStr";
});

It works when I add and delete myClass ' from myObservable . But when I change a property of myClass the changes are not reflected in the WPF DataGrid even though myObservable has had it's value changed.

What am I missing?

I've tried several other sources but to no avail. Strangely modifying the collection on a thread other than the UI thread seems to be possible. And I cannot percieve any difference in behavior when running on the new thread nor invoking it on the UI thread.

Your myClass needs to implement INotifyPropertyChanged. This is how a WPF data binding notifies the UI that properties change.

public class myClass: INotifyPropertyChanged
{
    private string _myStringOne;
    public string myStringOne { 
       get { return _myStringOne; } 
       set 
       {
           if(_myStringOne != value)
            {
                  _myStringOne = value;
                  NotifyPropertyChanged("myStringOne");
            }
      }
   }

   public event PropertyChangedEventHandler PropertyChanged;

   public void NotifyPropertyChanged(string propName)
   {
        if(this.PropertyChanged != null)
             this.PropertyChanged(this, new PropertyChangedEventArgs(propName));
   }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM