[英]Checkbox in Datagrid MVVM WPF
View ConfigRole具有兩列,即Name和IsEnabled(checkBox),每行都有Button Edit,
ConfigRole:
**如果ConfigRole視圖中某行的CheckBox為TRUE,當我單擊Edit Button時,我將得到新的View Button_Lists,它具有2列Name_Button和IsEnabled(CheckBox)...並且我為每個IsEnabled修改了TRUe或FALSe。
**我的對象是:當IsEnabled ConfigRole為FALSE時,我想將所有IsEnabled Button_Lists默認設置為FALSE,
ButtonList:
這是我在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中所有用於View Button_Lists的CheckBox默認為FALSE,
我該如何解決?
通過添加檢查_buttonList
是否為null
的if語句,確保已初始化_buttonList
。 如果擺脫了該異常,則說明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;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.