2017-06-07 236 views
0

观ConfigRole有2列,名称和的IsEnabled(复选框)与按钮编辑的每一行,复选框DataGrid中MVVM WPF

ConfigRole:

enter image description here

**如果复选框为TRUE在ConfigRole视图中的一行,当我点击编辑按钮时,我得到具有2列Name_Button和IsEnabled(CheckBox)的新的View Button_Lists ...并且我修改了每个IsEnabled的TRUe或FALSe。

**我OBJECTIF是:当的IsEnabled ConfigRole是假的,我想所有的IsEnabled Button_Lists默认为FALSE,

ButtonList:

enter image description here

这是Button_Lists我try代码型号:

// get all of ButtonsList 
public ObservableCollection<ButtonRoleMapClass> ButtonList 
    { 
     get { return _buttonList; } 
     set 
     { 
      _buttonList = value; 
      OnPropertyChanged("ButtonList"); 
     } 
    } 

    //viewRoleButton: the Name of selected row in ConfigRole 
    public ButtonListViewModel(ViewRoleMapClass viewRoleButton) 
    {    

     //If IsEnabled for row in ConfigRole is FALSE 
     if (viewRoleButton.IsEnabled==false) 
     { 
      ObservableCollection<ButtonRoleMapClass> butsList = new ObservableCollection<ButtonRoleMapClass>(); 
      foreach (ButtonRoleMapClass button in _buttonList) 
      { 
       button.IsEnabled = false; 
       butsList.Add(button); 
      } 
      _buttonList = butsList;     
     } 

我想获得查看Button_Lists默认DataGrid中所有的复选框为FALSE,

但是我的代码,我有这样的错误:enter image description here

我怎样才能解决这个问题?

+0

有什么错误?你可以给视图的代码吗? – Etienne

+0

使用ButtonList = butsList直接。那么你的PropertyChanged应该工作。 –

+0

不,_buttonList是值;所以_buttonList = butsList;是正确的 – devtunis

回答

2

确保您已通过添加if语句来初始化_buttonList,if语句检查_buttonList是否为null。如果你摆脱例外的,你知道ObservableCollection尚未初始化

你可能也想财产设置 uttonList到新的集合:

public ButtonListViewModel(ViewRoleMapClass viewRoleButton) 
{ 
    //If IsEnabled for row in ConfigRole is FALSE 
    if (viewRoleButton.IsEnabled == false) 
    { 
     ObservableCollection<ButtonRoleMapClass> butsList = new ObservableCollection<ButtonRoleMapClass>(); 
     if (_buttonList != null) 
     { 
      foreach (ButtonRoleMapClass button in _buttonList) 
      { 
       button.IsEnabled = false; 
       butsList.Add(button); 
      } 
     } 
     ButtonList = butsList; 
    } 
} 
+0

它现在有效,当我验证if(_buttonList!= null) 非常感谢:) – devtunis