我觉得我在这里遗漏了一些东西,但我有这个数据网格,当数据源发生变化时,它会自动重新绘制它,而没有这样做的逻辑原因。
我将数据网格绑定(bind)到实现 INotifyPropertyChanged 的 DataView 属性,我想在调用 Refresh() 之前触发该事件时做一些其他事情。
这是数据源。
public class MainScreenDataView : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
void OnPropertyChanged(string name)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(name));
}
}
DataView _dataview;
public DataView GetDataView
{
get { return _dataview; }
set
{
_dataview = value;
OnPropertyChanged("GetDataView");
}
}
public MainScreenDataView()
{
}
}
和绑定(bind)(我在窗口的构造函数中调用它)
public void MakeData()
{
MiddleMan midman = MiddleMan.Instance;
midman.PropertyChanged += new System.ComponentModel.PropertyChangedEventHandler(midman_PropertyChanged); //unrelated event for error messages
midman.InstantiateAll();
Binding bind = new Binding();
bind.Source = midman.GetDict["contact"].GetDataView; //GetDict is a dictionary that holds instances of MainScreenDataView
bind.UpdateSourceTrigger = UpdateSourceTrigger.Explicit;
DG_Contacts.SetBinding(BetterDataGrid.ItemsSourceProperty, bind);
}
用数据库中的数据更新 DataView 的类可以访问与窗口相同的 MainScreenDataView 实例。该实例以单例形式保存在字典中。
现在我看不出为什么数据网格会自行刷新,我什至尝试从 MainScreenDataview 中删除 INotifyPropertyChanged 内容,但它保持相同的行为。
猜猜我在这里遗漏了什么。某处的默认行为需要被覆盖还是什么?
最佳答案
你已经交换了目标和源。自己做的。 UpdateSourceTrigger.Explicit
设置会影响绑定(bind)如何更新 source,即 MainScreenDataView.GetDataView
属性而不是 DataGrid.ItemSource
。 DataGrid.ItemSource
是目标。
从 MainScreenDataView
中删除 INotifyPropertyChanged
对单例没有影响,因为实例不会改变,只会改变实例中的值。换句话说,GetDataView
是一个“设置后不用管”的属性。
只要绑定(bind)有效,就无法阻止绑定(bind)系统传播对集合所做的更改,除非您抑制 DataView.CollectionChanged
事件的触发或阻止,以便绑定(bind)子系统根本无法运行。
如果你真的想要这个,你可以断开绑定(bind)并在准备好时重新设置它,或者创建一个全新的 DataView
并在准备好时覆盖绑定(bind)。
关于c# - WPF 数据网格在不应该刷新时自行刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6084290/