简体   繁体   English

ObservableCollection的异步修改 <T> 未在DataGrid中表示

[英]Async modification of ObservableCollection<T> not represented in DataGrid

It's been a while since I've worked with Forms/WPF. 自从我使用Forms / WPF以来已经有一段时间了。 So excuse me if this is a stupid question. 如果这是一个愚蠢的问题,请原谅。

I have an ObservableCollection<myClass> named myObservable where myClass consists of the following: 我有一个名为myObservableObservableCollection<myClass> ,其中myClass包含以下内容:

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. 并在UI.xaml中进行以下控制。

<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. LoadLines是我在UI中阻止事物的功能。

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 . 当我从myObservable添加和删​​除myClass时,它可以工作。 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. 但是,当我更改myClass的属性时,即使myObservable的值已更改,更改也不会反映在WPF DataGrid中。

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. 似乎有可能在UI线程以外的其他线程上修改集合。 And I cannot percieve any difference in behavior when running on the new thread nor invoking it on the UI thread. 而且,我无法在新线程上运行或在UI线程上调用它时在行为上有任何区别。

Your myClass needs to implement INotifyPropertyChanged. 您的myClass需要实现INotifyPropertyChanged。 This is how a WPF data binding notifies the UI that properties change. 这就是WPF数据绑定如何通知UI属性更改的方式。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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