【问题标题】:Does Removing items from SelectedItems source remove them from the ItemsSource?从 SelectedItems 源中删除项目是否会从 ItemsSource 中删除它们?
【发布时间】:2013-10-30 19:54:34
【问题描述】:

如果我有 2 个ObservableCollections<someObject>,一个代表ItemsSource,一个代表SelectedItems 源。

SelectedItems 源上调用Clear() 函数是否会从ItemsSource 中删除这些项目?

【问题讨论】:

    标签: c# wpf observablecollection selecteditem


    【解决方案1】:

    不,它只会影响您的 SelectedItems 收藏(当然是清空它)。 Clear 只是从集合中删除项目而不是另一个:

    • ItemsSource 引用了一个包含对象引用的 ObservableCollection
    • SelectedItems 引用另一个 ObservableCollection 包含一些对象引用也包含在您的 ItemsSources 集合中;

    在您的SelectedItems 集合上调用Clear 方法只会删除包含在SelectedItems 中的对象引用,而不是对象(它们自身)或来自另一个集合(此处为ItemsSource)的对象引用。

    【讨论】:

    • 所以要从我的 ItemsSource 中删除我的 SelectedItems 中的每个项目,我需要循环吗?这就是我想要达到的目标
    • 是的,您需要在 ItemsSource 集合中找到相应的项目并将其删除。
    猜你喜欢
    • 2019-09-26
    • 1970-01-01
    • 2011-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-11
    • 1970-01-01
    相关资源
    最近更新 更多