WPF 数据网格在不应该刷新时自行刷新
我觉得我在这里遗漏了一些东西,但是我有这个数据网格,当数据源更改时,它会自动重新绘制它,而没有任何逻辑原因这样做。
我将数据网格绑定到实现 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()
{
}
}
并且绑定(我在窗口的构造函数中称之为)
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 内容,但它保持相同的行为。
猜猜我在这里缺少一些东西。某个地方的默认行为需要被覆盖还是什么?
I feel like I'm missing something here, but I have this datagrid which when the datasource changes, automatically redraws it self with no logical reason for it doing so.
I have the datagrid bound to a DataView property which implements INotifyPropertyChanged and I want to do some other stuff when that event is fired before calling Refresh().
So here is the datasource.
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()
{
}
}
And the binding (I call this in the constructor of the window)
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);
}
The class that updates the DataView with data from a database has access to that same instance of MainScreenDataView as the window. The instance is kept in a dictionary in a singleton.
Now I see no reason why the datagrid would refresh it self, I even tried removing the INotifyPropertyChanged stuff from MainScreenDataview and yet it keeps the same behavior.
Guess there's something I'm missing here. Default behavior somewhere that needs to be overridden or something?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您已经交换了目标和源。我自己做的。
UpdateSourceTrigger.Explicit
设置影响绑定更新源的方式,源是MainScreenDataView.GetDataView
属性,而不是DataGrid.ItemSource.
DataGrid.ItemSource
是目标。从
MainScreenDataView
中删除INotifyPropertyChanged
不会对单例产生任何影响,因为实例不会改变,只会改变实例内的值。换句话说,GetDataView
是一个“设置后就忘记它”的属性。只要绑定有效,就无法阻止绑定系统传播对集合所做的更改,除非您禁止触发或阻止
DataView.CollectionChanged
事件,以便绑定子系统根本不运行。如果您确实想要这样做,您可以断开绑定并在准备好时再次设置它,或者创建一个全新的 DataView 并在准备好时覆盖绑定。
You've got target and source swapped. Done it myself. The
UpdateSourceTrigger.Explicit
setting affects how the binding updates the source which is theMainScreenDataView.GetDataView
property not theDataGrid.ItemSource
. TheDataGrid.ItemSource
is the target.Removing
INotifyPropertyChanged
fromMainScreenDataView
will have no effect on a singleton because the instance doesn't change, only the values inside the instance. In other words,GetDataView
is a "set it and forget it" property.As long as the binding is in effect, there is no way to prevent changes made to the collection from being propagated by the binding system unless you suppress
DataView.CollectionChanged
events from firing or block so that the binding subsystem simply doesn't run.If you really want this you can disconnect the binding and set it again when you are ready or create an entirely new
DataView
and overwrite the binding when you are ready.