【发布时间】:2011-05-21 20:30:51
【问题描述】:
我觉得我在这里遗漏了一些东西,但是我有这个数据网格,当数据源发生变化时,它会自动重绘它,而没有任何逻辑理由这样做。
我将数据网格绑定到实现 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 内容,但它保持相同的行为。
猜猜我在这里遗漏了一些东西。某处需要覆盖的默认行为或其他什么?
【问题讨论】:
标签: c# wpf data-binding datagrid dataview