Reputation: 13
I have some strange problem in my WPF app. I'm using a MVVM pattern and this is the part of my 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
}
When I call OnCreateTable() program almost allways hangs up with 100% CPU usage (sometimes with no CPU usage but others errors like incorrect data in GridView control).
While debugging I discovered some facts:
1) OnCreateTable() and data bindings work fine if make pause before OnPropertyChanged:
void OnCreateTable()
{
_dt = // creating new table here
Thread.Sleep(1000); //!!!
OnPropertyChanged("DT"); // refresh binding
}
2) OnCreateTable() and data bindings work fine if trace it with "step over" (because this makes pause before OnPropertyChanged too)
I can't understand why I need to make pause before OnPropertyChanged.
Upvotes: 0
Views: 184
Reputation: 13
I think I found the problem. Sorry, I forgot that I have added the property name checking:
public void OnPropertyChanged(string propertyName)
{
VerifyPropertyName(propertyName);
if (PropertyChanged != null)
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
[Conditional("DEBUG")]
public void VerifyPropertyName(string propertyName)
{
// Verify that the property name matches a real,
// public, instance property on this object.
if (TypeDescriptor.GetProperties(this)[propertyName] == null)
{
throw new Exception("Invalid property!");
}
}
I can't understand why, but calling VerifyPropertyName() needs a pause, or it results in that bug, I wrote. If I remove call to VerifyPropertyName() all works propertly!
Upvotes: 0
Reputation: 45096
Try setting the public property. It is kind of a reach but too much for a comment.
public DataTable DT
{
get { return _dt; }
set
{
if(_dt == value) return;
_dt = value;
OnPropertyChanged("DT");
}
}
DT = // creating new table here
Upvotes: 1