简体   繁体   English

在自定义控件中通过IEnumerable订阅INotifyCollection Changed事件

[英]Subscribing to INotifyCollection Changed event through IEnumerable in Custom Control

I am not really sure how to ask this question - therefore I also don't know how to search for a proper answer. 我不确定如何问这个问题-因此我也不知道如何寻找正确的答案。 Here is the simplistic scenario. 这是一个简单的场景。
1. I have a Custom Control (myControl) with a property Called myItemsSource which is of Type IEnumerable - I need this so that when I use the control I can use either a list / observable collection / IEnumerable etc. 2. My property then use a wrapper for the logic of this IEnumerable List. 1.我有一个自定义控件(myControl),其属性名为myItemsSource,类型为IEnumerable-我需要这样做,以便在使用控件时可以使用列表/可观察的集合/ IEnumerable等。2.然后使用我的属性此IEnumerable List逻辑的包装器。 The wrapper have a few Ovservable lists / normal lists which then is bound in the background to several ItemsControl elemments on myControl. 包装器具有一些Ovservable列表/普通列表,然后将它们在后台绑定到myControl上的几个ItemsControl元素。

Therefore bare in mind that when I use myControl in an application, I only bind a list once as an IEnumerable. 因此,请记住,当我在应用程序中使用myControl时,我仅将列表作为IEnumerable绑定一次。 When I bind a Observable Collection I would like the same functionality (ie notification to the user interface - therefore how do I notify my wrapper that the Items have changed in the list? I cannot subscribe to an event in my wrapper, because I used IEnumerable. This means that even if I use Observablecollections for lists inside my wrapper and bind several controls to these lists.... they will not be updated because my wrapper was not notified that the items have changed? however I know the items even in my wrapper has been added... how do I send the notification to my wrapper so that it can recalculate the list - update several other lists which will update my user interface? 当我绑定一个Observable Collection时,我希望具有相同的功能(即通知用户界面-因此,如何通知包装器列表中的项已更改?我无法在包装器中订阅事件,因为我使用了IEnumerable这意味着即使我在包装器内的列表中使用Observablecollections并将多个控件绑定到这些列表....它们也不会更新,因为我的包装器未收到有关项目已更改的通知?包装器已添加...我如何将通知发送到包装器,以便它可以重新计算列表-更新其他几个列表以更新用户界面?

Alternative question... how do you see if an IEnumerable list also implements INotifyCollectionChanged through 1 method [ method(IEnumerable list); 替代问题...您如何看待IEnumerable列表是否还通过1方法实现INotifyCollectionChanged [method(IEnumerable list); ] -> this might get me to the right place. ]->这样可能会把我带到正确的地方。

Thanks to Khan below I came to the following solution (I type it here as it is more simplistic to see the answer this way). 感谢下面的Khan,我来到了以下解决方案(我在这里键入它是因为以这种方式查看答案更加简单)。 PLEASE Vote him up as his quick response helped me greatly!! 请投票给他,因为他的快速反应对我有很大帮助!

public class Wrapper
{
    INotifyCollectionChanged INC;

    public Wrapper(IEnumerable list)
    {

        if (list is INotifyCollectionChanged)
        {
            INC = list as INotifyCollectionChanged;
            INC.CollectionChanged += INC_CollectionChanged;
        }
    }

    void INC_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
    { 
        // Some Code ;
    }
}

For your solution, couldn't you make myItemsSource an ObservableCollection<T> and avoid your situation? 对于您的解决方案,您不能将myItemsSourceObservableCollection<T>并避免出现这种情况吗?

how do you see if an IEnumerable list also implements INotifyCollectionChanged 您如何查看IEnumerable列表是否还实现了INotifyCollectionChanged

You can check if an entity implements an interface by using the is keyword. 您可以使用is关键字检查实体是否实现了接口。

var myCollection = new ObservableCollection<string>();
var isINotifyCollectionChanged = (myCollection is IsNotifyCollectionChanged);

If you want a specific method for your case: 如果您要针对您的情况使用特定方法:

bool IsINotifyCollectionChanged<T>(IEnumerable<T> collection)
{
    return (collection is INotifyCollectionChanged);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM