C# viewmodel属性中的更改未反映在UI中
我是WPF新手,我实现了C# viewmodel属性中的更改未反映在UI中,c#,wpf,C#,Wpf,我是WPF新手,我实现了INotifyPropertyChanged接口。我有一个包含属性“TeamMemberList”的viewmodel。该控件执行setter部分,更改属性值,但PropertyChanged事件保持为null。 以下是代码: ViewModelBase: public class ViewModelBase : INotifyPropertyChanged { public event PropertyChangedEventHandler PropertyCh
INotifyPropertyChanged
接口。我有一个包含属性“TeamMemberList”的viewmodel。该控件执行setter部分,更改属性值,但PropertyChanged事件保持为null。
以下是代码:
ViewModelBase:
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
protected void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
}
ViewModel:(它继承viewmodelbase)
财产是
public List<Employee> TeamMemberList
{
get
{
return _teamMemberList;
}
set
{
_teamMemberList = value;
NotifyPropertyChanged("TeamMemberList");
}
}
员工培训与实施
public class Employee : ViewModelBase
{
private string _employeeName;
private string _employeeId;
private string _profilePic;
private string _designation;
private string _reportinManager;
public string EmployeeName
{
get
{
return _employeeName;
}
set
{
_employeeName = value;
NotifyPropertyChanged("EmployeeName");
}
}
public string EmployeeId
{
get
{
return _employeeId;
}
set
{
_employeeId = value;
NotifyPropertyChanged("EmployeeId");
}
}
public string ProfilePic
{
get
{
return _profilePic;
}
set
{
_profilePic = value;
NotifyPropertyChanged("ProfilePic");
}
}
public string Designation
{
get
{
return _designation;
}
set
{
_designation = value;
NotifyPropertyChanged("Designation");
}
}
public string ReportingManager
{
get
{
return _reportinManager;
}
set
{
_reportinManager = value;
NotifyPropertyChanged("ReportingManager");
}
}
}
使用ObserveableCollection而不是列表当我们看不到更多代码(例如:如何设置DataContext等)时,很难说问题出在哪里 但是,通过向绑定添加以下属性,可以很容易地调试绑定:
<ListBox Margin="10" ItemsSource="{Binding TeamMemberList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
PresentationTraceSources.TraceLevel=High>
添加此属性将把整个绑定序列输出到VisualStudio的输出窗口。这应该能指出哪里出了问题
如果要对所有绑定启用此选项,还可以使用Visual Studio选项:
除非您为其分配了一个委托,否则该值将为空。。更新绑定不是问题。。显示要更新的代码以及模型(Employee类)的实现。@Sankarann-
除非为该模型指定了一个委托,否则该值将为空。您的意思是什么?@user3217737-您是否将Viewmodel设置为视图的DataContext?@RohitVats:对于绑定的更新,它不需要一个值,对于处理已更改属性的内部使用,PropertyChanged
可以分配一个委托。但OP希望它仅用于绑定目的。因此,不需要分配委托。虽然列表不允许像ObservableCollection那样进行丰富的交互,但它应该仍然有效,所以我认为这不是解决方案
public class Employee : ViewModelBase
{
private string _employeeName;
private string _employeeId;
private string _profilePic;
private string _designation;
private string _reportinManager;
public string EmployeeName
{
get
{
return _employeeName;
}
set
{
_employeeName = value;
NotifyPropertyChanged("EmployeeName");
}
}
public string EmployeeId
{
get
{
return _employeeId;
}
set
{
_employeeId = value;
NotifyPropertyChanged("EmployeeId");
}
}
public string ProfilePic
{
get
{
return _profilePic;
}
set
{
_profilePic = value;
NotifyPropertyChanged("ProfilePic");
}
}
public string Designation
{
get
{
return _designation;
}
set
{
_designation = value;
NotifyPropertyChanged("Designation");
}
}
public string ReportingManager
{
get
{
return _reportinManager;
}
set
{
_reportinManager = value;
NotifyPropertyChanged("ReportingManager");
}
}
}
<ListBox Margin="10" ItemsSource="{Binding TeamMemberList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
PresentationTraceSources.TraceLevel=High>