如果我们创建完整属性,ICollectionView的CurrentChanged事件不起作用

时间:2014-01-29 12:28:54

标签: c# wpf

当我们创建ICollectionView的自动属性时,刷新Employee集合后,CurrentChanged事件正常工作。

public ICollectionView EmployeeCollectionView{get; set; }

public EmployeeMasterViewModel(IEmployeeMasterView view, IUnityContainer container)
{
 GetEmployee();
 EmployeeCollectionView.CurrentChanged += new EventHandler(EmployeeCollectionView_CurrentChanged);
}

当我们创建full-property时,CurrentChanged事件无效。

private ICollectionView _employeeCollectionView;
public ICollectionView EmployeeCollectionView
 {
   get { return _employeeCollectionView; }
   set { _employeeCollectionView = value; OnPropertyChanged("EmployeeCollectionView");}
 }


public EmployeeMasterViewModel(IEmployeeMasterView view, IUnityContainer container)
{
   GetEmployee();
  EmployeeCollectionView.CurrentChanged += new EventHandler(EmployeeCollectionView_CurrentChanged);
}

 void EmployeeCollectionView_CurrentChanged(object sender, EventArgs e)
   {
    var currentEmployee = EmployeeCollectionView.CurrentItem as EmployeeMaster;
   }

如果我错过了什么,请建议。

3 个答案:

答案 0 :(得分:0)

您是否尝试将活动分配给私人会员? 你是如何在xaml文件中完成绑定的? 我会评论,但我没有足够的XP: - (

答案 1 :(得分:0)

刷新员工代答后,您是否绑定了EmployeeCollectionView_CurrentChanged事件?因为如果刷新员工集合,则EmployeeCollectionView_CurrentChanged连接已丢失。

像 -

private void Refresh()
 {
   GetEmployee();
   EmployeeCollectionView.CurrentChanged += new EventHandler(EmployeeCollectionView_CurrentChanged);
}

答案 2 :(得分:0)

如果您希望更改EmployeeCollectionView(看起来如此,否则您不需要OnPropertyChanged,我建议您在您的媒体资源中添加事件,如下所示:< / p>

private ICollectionView _employeeCollectionView;

public ICollectionView EmployeeCollectionView
{
  get { return _employeeCollectionView; }
  set
  {
    if (_employeeCollectionView != value)
    {
      if (_employeeCollectionView != null)
      {
        _employeeCollectionView.CollectionChanged -= EmployeeCollectionView_CurrentChanged;
      }
      _employeeCollectionView = value;
      _employeeCollectionView.CollectionChanged += EmployeeCollectionView_CurrentChanged;
      OnPropertyChanged("EmployeeCollectionView");
    }

  }
}


public EmployeeMasterViewModel(IEmployeeMasterView view, IUnityContainer container)
{
  GetEmployee();      
}

private void EmployeeCollectionView_CurrentChanged(object sender, EventArgs e)
{
  var currentEmployee = EmployeeCollectionView.CurrentItem as EmployeeMaster;
}