反转ObservableCollection的布尔值

本文关键字:布尔值 ObservableCollection 反转 | 更新日期: 2023-09-27 18:22:08

我只是想反转ObservableCollection的一个名为ActiviteCollection的布尔值。

这个布尔值被自动定义为False(0),但当我点击按钮时,一些布尔值会变成true。这是我无法成功执行的反向操作,我在网上也找不到这样的操作。

提前感谢您的帮助。

如果你想要一些代码,我可以编辑我的帖子,但反转ObservableCollection的布尔值的通用方法应该足以帮助我找到目标。

这是按钮的脚本,它将反转布尔值:

private void click_PrisEnCompte(object sender, RoutedEventArgs e)
{
    var viewModel = (ViewModel)(sender as System.Windows.Controls.ListBoxItem).DataContext;
    foreach (var data in DonneesBrutes.SelectedItems)
    {
        if(viewModel.ActiviteCollection.Any(c => c.PMRQTOTMActivite == DonneesBrutes.CurrentItem.ToString()))
        {
            var reversedCollection = viewModel.ActiviteCollection.Select(t => !t);
        }
    }
}

此脚本当前不起作用,在Select:The type Arguments for method ... cannot be inferred for the usage.和上出现错误!t Cannot apply Operator ! to operand of type.....

反转ObservableCollection的布尔值

foreach (var data in DonneesBrutes.SelectedItems)
{
    foreach (var item in viewModel.ActiviteCollection.Where(c => c.PMRQTOTMActivite == DonneesBrutes.CurrentItem.ToString()))
    {
        item.MyBoolean = !item.MyBoolean;
    }
}
bool b = false;
b = !b; //Inverted!!

在可观测集合中

ObservableCollection<Bool> BooleanList;
foreach(bool b in BooleanList)
    b = !b;

以下将"反转"集合中的每个项目:

var reversedCollection = from t in ActiviteCollection select !t;

如果您想"反转"给定索引处的布尔值,可以执行以下操作:

ActiviteCollection[index] = !ActiviteCollection[index];