我的GUI应用程序上有多个复选框,可以为相同类型的每个对象启用自动更新。因此,如果选中该复选框,isautoupdate属性将设置为true,否则将设置为false。我有一个按钮,需要启用/禁用所有复选框的自动更新。如何检查所有对象的isautoupdate属性是否设置为true或false。
我目前的实现是使用foreach循环,它遍历每个对象并检查isautoupdate是否设置为true或false,但我得到了切换效果,如果选中了一些复选框,它将取消选中它们,反之亦然。
在.cs中
foreach (MxL_GUI_ChannelSettingAndStatusItem item in theGUIManager.theDevice.channelCollection)
{
if (!item.IsAutoUpdated)
{
item.IsAutoUpdated = true;
}
else
{
item.IsAutoUpdated = false;
}
}
发布于 2013-03-29 20:05:17
如果你不想让你的从属复选框切换,那么就不要编写切换它们的代码。而应选中主复选框的IsChecked
属性,并将该值应用于项目的所有IsAutoUpdated
属性:
foreach (MxL_GUI_ChannelSettingAndStatusItem item in ...)
{
item.IsAutoUpdated = masterCheckbox.IsChecked.Value;
}
发布于 2013-03-29 20:20:11
我不确定我是否准确地理解了你的要求。如果要检测所有项是否都设置为true或false,请使用:
var items = theGUIManager.theDevice.channelCollection;
// If you need to know if for all items IsAutoUpdated = true
bool allChecked = items.All(item => item.IsAutoUpdated);
// If you need to know if they're all false
bool noneChecked = !items.Any(item => item.IsAutoUpdated);
然后更新你的项目,例如:
foreach(var item in items) { item.IsAutoUpdated = !allChecked; }
https://stackoverflow.com/questions/15710549
复制