0

The View ConfigRole has 2 columns, Name and IsEnabled(checkBox)with Button Edit for every row,

ConfigRole:

enter image description here

** If CheckBox is TRUE for a Row in ConfigRole View,when I click on Edit Button I get new View Button_Lists which has 2 columns Name_Button and IsEnabled(CheckBox)...and I modify TRUe or FALSe for every IsEnabled.

**My objectif is : When IsEnabled ConfigRole is FALSE, I want to get all the IsEnabled Button_Lists default to FALSE,

ButtonList:

enter image description here

this is my try code in Button_Lists Model:

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

I want to get all CheckBox in Datagrid for View Button_Lists default is FALSE,

But with my code, I have this Error:enter image description here

How can I fix it?

4
  • what's the error ? can you give the code of the view as well ? Commented Jun 7, 2017 at 2:00
  • Use ButtonList = butsList direct. Then your PropertyChanged should work. Commented Jun 7, 2017 at 6:22
  • No, _buttonList is the value;so _buttonList = butsList; is correct Commented Jun 7, 2017 at 8:03
  • Do you set the value of _buttonList anywhere? If so its initial value is null. Commented Jun 7, 2017 at 8:07

1 Answer 1

2

Make sure that you have initialized the _buttonList by adding an if statement that check whether _buttonList is null. If you get rid of the exception, you know that the ObservableCollection hasn't been initialized

You probably also want to set the ButtonList property to the new collection:

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

It works now,when I verify if (_buttonList != null) Thanks a lot :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.