Datagrid MVVM WPF中的复选框

时间:2017-06-07 00:17:04

标签: c# wpf mvvm

View ConfigRole有两列,Name和IsEnabled(checkBox),每行有Button Edit,

ConfigRole:

enter image description here

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

**我的目标是:当IsEnabled ConfigRole为FALSE时,我想让所有IsEnabled Button_Lists默认为FALSE,

ButtonList:

enter image description here

这是我在Button_Lists模型中的尝试代码:

 // 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;                
        }   

我想获取Datagrid中的所有CheckBox for View Button_Lists默认为FALSE,

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

我该如何解决?

1 个答案:

答案 0 :(得分:2)

通过添加if语句来确保_buttonList_buttonList,确保您已初始化null。如果你摆脱了异常,你知道ObservableCollection尚未初始化

您可能还想将 B 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;
    }
}