引发PropertyChanged事件时出现WPF问题 [英] WPF problem when Raising PropertyChanged event
问题描述
Hello all
我一直在使用WPF应用程序,它完成了99%,但我遇到了问题。
当我在数据库删除操作后尝试更新DataGrid时,我得到以下异常:
非静态方法需要一个目标
当View Model试图更新视图时,ViewModelBase内发生异常,代码:
public class ViewModelBase:INotifyPropertyChanged
{
public ViewModelBase()
{
}
#region INotifyPropertyChanged成员
public event PropertyChangedEventHandler PropertyChanged ;
#endregion
#region私有方法
public void RaisePropertyChanged( string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler!= null )
{
handler( this , new PropertyChangedEventArgs(propertyName)); // 此处例外
}
}
#endregion
其余时间应用按预期运行,代码部分没有问题。数据从数据库返回没有任何问题。
有没有人有任何想法?
在此先感谢。
Baxter-P
好的,
感谢您的回复。
关于细节的评论非常公平,所以这里是酒店我绑定到:
私有ObservableCollection < UserData > userData = new ObservableCollection < UserData > ();
public ObservableCollection < UserData > UserData
{
get
{
return userData;
}
设置
{
userData = value;
RaisePropertyChanged(UserData);
}
}
此处还有数据类:
public class UserData
{
public UserData( int id, string userName)
{
this .ID = id;
this .UserName = userName;
}
public int ID {获得跨度>; set ; }
public string UserName { get 跨度>; set ; }
}
标准的东西。这里还有我的DataGrid XAML代码的屏幕截图:
http://s30.postimg.org/rftcsp01d/xaml.png [ ^ ]
这里是导致例外情况的代码:
private void DeleteUserCommandExecute(对象参数)
{
WPFMessageBox messageBox = new WPFMessageBox( 删除?, 你确定你想要删除用户' + userName + '?,MessageBoxType.YesNo) ;
if (messageBox.Answer!= MessageBoxDecision.Yes)
return 跨度>;
try
{
lookUpContext.DeleteUser(userID);
lookUpContext.SaveChanges();
RemoveAllUserDivisions();
PopulateUserDivisions();
GetUserData(); // 此处异常
UserName = 字符串跨度> .Empty;
AllowUIToUpdate();
}
catch (例外情况)
{
WPFMessageBox errorMessageBox = 新 WPFMessageBox( 错误, 错误删除数据 - + ex.Message,MessageBoxType.OK);
}
}
此外,对数据库的调用:
private void GetUserData()
{
lookUpContext = new JMLookupEntities();
var UserDataLocal =( from data in lookUpContext.JMUsers.ToList()
选择 new UserData( data.ID,data.UserName))。ToList();
UserData = UserDataLocal.ToObservableCollection(); // 这会更新视图,或者应该更新..
}
导致异常的事件是DeleteUserCommand执行事件。当我尝试更新上面显示的'dgUsers'数据源属性'UserData'时发生异常。在问题发生之前多次调用更新视图的代码块,没有问题。
我真的不明白这个问题,我已经尝试了一切。
我使用的是WPF 4.5和EF 6.
再次感谢,
Baxter-P
我有各种解决方案。
我将控件从DataGrid更改为ListView,虽然我仍然得到异常,但控件现在更新。
所以我将异常隐藏在一个try / catch块,有一些逻辑只能隐藏specfific异常,而不是所有异常。
所以我的应用程序已完全完成,感谢您的支持。
我在StackOverflow上找到了以下内容(我用Google搜索非静态方法需要一个目标),这是第一次点击:
http://stackoverflow.com/questions/13717355/non-static-method-requires-a-target [ ^ ]
它似乎是由于linq执行中的空引用异常。
Hello all
I have been working on a WPF app, and it's 99% finished, but I have a problem.
When I attempt to update a DataGrid after a database delete operation, I get the following, exception:
Non-static method requires a target
The Exception is happening inside the ViewModelBase when the View Model attempts to update the view, code:
public class ViewModelBase : INotifyPropertyChanged
{
public ViewModelBase()
{
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
#region Private Methods
public void RaisePropertyChanged(string propertyName)
{
PropertyChangedEventHandler handler = PropertyChanged;
if (handler != null)
{
handler(this, new PropertyChangedEventArgs(propertyName)); // Exception here
}
}
#endregion
The rest of the time the app runs as expected, with the code section causing no problems. The data comes back from the database without any problems.
Has anyone got any ideas ??
Thanks in advance.
Baxter-P
OK,
Thanks for the replies.
The comments about details are very fair, so, here is the property I am binding to:
private ObservableCollection<UserData> userData = new ObservableCollection<UserData>();
public ObservableCollection<UserData> UserData
{
get
{
return userData;
}
set
{
userData = value;
RaisePropertyChanged("UserData");
}
}
Also here is the class for the data:
public class UserData
{
public UserData(int id, string userName)
{
this.ID = id;
this.UserName = userName;
}
public int ID { get; set; }
public string UserName { get; set; }
}
Standard stuff. Also here is a screen shot of my DataGrid XAML code:
http://s30.postimg.org/rftcsp01d/xaml.png[^]
Also here is the code where the excepetion is caused:
private void DeleteUserCommandExecute(Object parameter)
{
WPFMessageBox messageBox = new WPFMessageBox("Delete ?", "Are you sure you want to DELETE user '" + userName + "' ?", MessageBoxType.YesNo);
if (messageBox.Answer != MessageBoxDecision.Yes)
return;
try
{
lookUpContext.DeleteUser(userID);
lookUpContext.SaveChanges();
RemoveAllUserDivisions();
PopulateUserDivisions();
GetUserData(); // Exception here
UserName = String.Empty;
AllowUIToUpdate();
}
catch (Exception ex)
{
WPFMessageBox errorMessageBox = new WPFMessageBox("Error", "Error DELETING data - " + ex.Message, MessageBoxType.OK);
}
}
Also, the call to the database:
private void GetUserData()
{
lookUpContext = new JMLookupEntities();
var UserDataLocal = (from data in lookUpContext.JMUsers.ToList()
select new UserData(data.ID, data.UserName)).ToList();
UserData = UserDataLocal.ToObservableCollection(); // This updates the view, or should do..
}
The event causing the exception is the DeleteUserCommand execute event. The exception occurs when I try to update the 'dgUsers' datasource property 'UserData' shown above. The code block to update the view is called multiple times before the problem occurs, with no problems.
I really do not understand this problem, and I have tried everything.
I am using WPF 4.5, and EF 6.
Thanks Again,
Baxter-P
I have a solution of sorts.
I changed the control to a ListView from a DataGrid, and although I still got the exception the control now updates.
So I hid the exception inside a try/catch block, with some logic to only hide the specfific exception, not all exceptions.
So my app is fully finished, thanks for the support.
I found the following on StackOverflow (I Googled "Non-static method requires a target") and this was the first "hit":
http://stackoverflow.com/questions/13717355/non-static-method-requires-a-target[^]
It appears to be due to a null reference exception in the linq execution.
这篇关于引发PropertyChanged事件时出现WPF问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!