视图模型属性中的更改未在UI中反映出来

时间:2014-04-15 06:50:18

标签: c# wpf

我是WPF新手我实现了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");
        }
    }

结合

<ListBox Margin="10" ItemsSource="{Binding TeamMemberList, Mode=TwoWay, 
                                   UpdateSourceTrigger=PropertyChanged}" >

当新员工加入数据库时​​,模型会将其读入&amp;为所有emplyee创建List,然后更新TeamMeberList属性。这是TeamMemberList的更新方法

    var qryEmp = from employee in ClientModel.EmployeeList
                         where employee.ReportingManager == UserProfile.EmployeeId
                         select new Employee
                         {
                             EmployeeId = employee.EmployeeId,
                             EmployeeName = employee.EmployeeName,
                             Designation = employee.Designation,
                             ProfilePic = employee.ProfilePic,
                         };
            TeamMemberList = qryEmp.ToList();

并实施员工

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");
        }
    }
}

2 个答案:

答案 0 :(得分:1)

当我们没有看到更多代码时(例如:如何设置DataContext等等)很难说出问题是什么。

但是有一种简单的方法可以通过向其添加以下属性来调试绑定:

<ListBox Margin="10" ItemsSource="{Binding TeamMemberList, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" 
         PresentationTraceSources.TraceLevel=High>

添加此属性会将整个绑定序列输出到Visual Studio的“输出”窗口。这应该指出出了什么问题。

如果要为所有绑定启用此功能,还可以使用Visual Studio选项:

Visual Studio Options dialog to enable WPF Tracing

答案 1 :(得分:0)

使用ObserveableCollection而不是列表