我觉得我在这里缺少了什么,但是我有一个数据网格,当数据源更改时,它会自动重新绘制,没有逻辑原因可以解释它这样做。
我将datagrid绑定到一个实现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的内容,但它仍然保持相同的行为。
猜想我在这里漏掉了某些东西。是否需要覆盖某个默认行为或者其他操作?
我明白了...但还是有点困惑。我猜我不需要使用Binding对象,我可以直接做 "DataGrid.ItemsSource = pathtodataview;",但即使这样它也会自动刷新。
我想在 "MainScreenDataView" 的 "propertychanged" 事件中执行刷新逻辑,所以解除绑定和重新绑定并不能真正起作用,因为网格在事件触发之前就开始重绘了。 - Steinthor.palsson