【问题标题】:Get a reference to the ObservableCollection that fired the change event获取对触发更改事件的 ObservableCollection 的引用
【发布时间】:2015-02-03 05:24:56
【问题描述】:

ObservableCollection 公开 CollectionChanged 事件。从该事件的处理程序有没有办法获得对触发事件的 ObservableCollection 的引用?我原以为 sender 参数是 ObservableCollection,但事实并非如此。

此代码示例说明 10 个 ObservableCollections 都将其 CollectionChanged 事件注册到一个方法。从那一种方法中,我想获得对已更改的 ObservableCollection 的引用:

internal class Program
{
    private static void Main(string[] args)
    {
        List<ObservableCollection<int>> collections = new List<ObservableCollection<int>>();

        for (int i = 0; i < 10; i++)
        {
            ObservableCollection<int> collection = new ObservableCollection<int>();
            collection.CollectionChanged += CollectionOnCollectionChanged;
            collections.Add(collection);
        }

        collections[5].Add(1234);
    }

    private static void CollectionOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
    {
        if (notifyCollectionChangedEventArgs.Action == NotifyCollectionChangedAction.Add)
        {
            // Before proceeding I need to get a reference to the ObservableCollection<int> where the change occured which fired this event.
        }
    }
}

查看传递给事件处理程序的参数,我没有看到对 ObservableCollection 的引用,所以我假设我无法得到它。

【问题讨论】:

  • 我错了!我在调试器中检查发送者实例的速度太快了。它看起来像是对 item 的引用,但它是 ObservableCollection 引用,我一直期望它是。

标签: c# .net events observablecollection


【解决方案1】:

sender 对象是触发事件的ObservableCollection 的实例。你可以投射它。

 private static void CollectionOnCollectionChanged(object sender, NotifyCollectionChangedEventArgs notifyCollectionChangedEventArgs)
 {
     if (notifyCollectionChangedEventArgs.Action == NotifyCollectionChangedAction.Add)
     {
         ObservableCollection<int> myCollection = sender as ObservableCollection<int>;
         if(myCollection != null){
             //do whatever you want
         }
     }
 }

【讨论】:

    【解决方案2】:

    您还有一个选择是使用内联委托并在集合上形成一个闭包,如下所示:

    private static void Main(string[] args)
    {
        List<ObservableCollection<int>> collections = new List<ObservableCollection<int>>();
    
        for (int i = 0; i < 10; i++)
        {
            ObservableCollection<int> collection = new ObservableCollection<int>();
            collection.CollectionChanged += (s, e) =>
            {
                if (e.Action == NotifyCollectionChangedAction.Add)
                {
                    // Can reference `collection` directly here
                }
            };
            collections.Add(collection);
        }
    
        collections[5].Add(1234);
    }
    

    sender 对象的存档转换(我觉得很乱。)

    另一种写法可能是:

    private static void Main(string[] args)
    {
        List<ObservableCollection<int>> collections =
            Enumerable
                .Range(0, 10)
                .Select(n => new ObservableCollection<int>())
                .ToList();
    
        collections
            .ForEach(collection =>
            {
                collection.CollectionChanged += (s, e) =>
                {
                    if (e.Action == NotifyCollectionChangedAction.Add)
                    {
                        // Can reference `collection` directly here
                    }
                };
            });
    
        collections[5].Add(1234);
    }
    

    【讨论】:

    • 好建议,干杯!一旦我的声望达到 15,我就会投票。
    猜你喜欢
    • 1970-01-01
    • 2017-05-07
    • 1970-01-01
    • 1970-01-01
    • 2013-05-01
    • 2020-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多