代码之家  ›  专栏  ›  技术社区  ›  Kraenys

反转ObservableCollection的布尔值

  •  -1
  • Kraenys  · 技术社区  · 11 年前

    我只是想反转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);
            }
        }
    }
    

    此脚本当前不工作,在选择时出错: The type Arguments for method ... cannot be inferred for the usage. 等等!吨 Cannot apply Operator ! to operand of type.....

    4 回复  |  直到 11 年前
        1
  •  2
  •   Weyland Yutani    11 年前
    foreach (var data in DonneesBrutes.SelectedItems)
    {
        foreach (var item in viewModel.ActiviteCollection.Where(c => c.PMRQTOTMActivite == DonneesBrutes.CurrentItem.ToString()))
        {
            item.MyBoolean = !item.MyBoolean;
        }
    }
    
        2
  •  0
  •   Alex Anderson    11 年前
    bool b = false;
    b = !b; //Inverted!!
    

    在可观察的集合中

    ObservableCollection<Bool> BooleanList;
    foreach(bool b in BooleanList)
        b = !b;
    
        3
  •  0
  •   joym8    11 年前

    以下内容将“反转”集合中的每个项目:

    var reversedCollection = from t in ActiviteCollection select !t;

        4
  •  0
  •   Weyland Yutani    11 年前

    如果要在给定索引处“反转”布尔值,可以执行以下操作:

    ActiviteCollection[index] = !ActiviteCollection[index];