我正在创建一个应用程序,该应用程序在网格视图中显示许多进程的状态。我遇到了一个问题,即当网格视图所代表的对象的数据正在更新时,网格视图无法“自动”更新行。从对DataBinding的Eto.Forms文档中的理解,如果一个对象实现了INotifyPropertyChanged
接口,则网格将在对象更改时自动刷新对象的字段(假设它们实际上是在通知)。
我创建了以下项目进行测试,发现只有在A)鼠标悬停,B)单击或C)在行上击键时,列才会更新。
以下是我的测试设置。就这么简单。
当项目属性更改时,如何使gridview自动更新?
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Timers;
using Eto.Forms;
//using Testing.Annotations;
namespace Testing
{
internal class Program
{
[STAThread]
public static void Main(string[] args)
{
new Application().Run(new TestForm());
}
}
public class TestForm : Form
{
public TestForm()
{
var gridView = new GridView<TestObj>();
gridView.Columns.Add(new GridColumn
{
HeaderText = "Test Field",
DataCell = new TextBoxCell {Binding = Binding.Property<TestObj, string>(item => item.TestField)}
});
Content = gridView;
var objects = new ObservableCollection<TestObj>
{
new TestObj(),
new TestObj(),
new TestObj()
};
gridView.DataStore = objects;
}
}
public class TestObj : INotifyPropertyChanged
{
private string _testField;
public string TestField
{
get => _testField;
set
{
if (value != _testField)
{
_testField = value;
OnPropertyChanged();
}
}
}
public TestObj()
{
var timer = new Timer(500);
var r = new Random();
timer.Elapsed += (sender, args) => TestField = r.Next().ToString();
timer.Start();
}
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}