【发布时间】:2012-03-01 22:04:34
【问题描述】:
我的 WPF 应用程序中有一些奇怪的问题。 我正在使用 MVVM 模式,这是我的 MainWindowViewModel 的一部分:
// GridView control in MainWindow.xaml binded to this property
public DataTable DT
{
get { return _dt; }
}
// INotifyPropertyChanged Member for refreshing bindings
public void OnPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
// my function
void OnCreateTable()
{
_dt = // creating new table here
OnPropertyChanged("DT"); // refresh binding
}
当我调用 OnCreateTable() 程序时,几乎总是以 100% 的 CPU 使用率挂起(有时没有 CPU 使用率,但其他错误,如 GridView 控件中的错误数据)。
在调试时我发现了一些事实:
1) 如果在 OnPropertyChanged 之前暂停,则 OnCreateTable() 和数据绑定工作正常:
void OnCreateTable()
{
_dt = // creating new table here
Thread.Sleep(1000); //!!!
OnPropertyChanged("DT"); // refresh binding
}
2) OnCreateTable() 和数据绑定可以正常工作,如果使用“step over”跟踪它(因为这也会在 OnPropertyChanged 之前暂停)
我不明白为什么我需要在 OnPropertyChanged 之前暂停。
【问题讨论】:
-
你有任何线程吗?
-
不,我没有。它的应用程序很简单。
-
您的数据表中有多少行?如果您直接设置 itemssource 而不绑定会发生什么(仅用于测试)
标签: wpf data-binding mvvm inotifypropertychanged