简体   繁体   中英

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. The wrapper have a few Ovservable lists / normal lists which then is bound in the background to several ItemsControl elemments on myControl.

Therefore bare in mind that when I use myControl in an application, I only bind a list once as an 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?

Alternative question... how do you see if an IEnumerable list also implements INotifyCollectionChanged through 1 method [ 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). 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?

how do you see if an IEnumerable list also implements INotifyCollectionChanged

You can check if an entity implements an interface by using the is keyword.

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);
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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