Peter Huber
Peter Huber

Reputation: 3312

WPF: What can be used as Source for Collection​View​Source

I would like to know, which classes and/or interfaces can be assigned to the WPF Collection​View​Source.​Source Property. The help documentation doesn't explain anything:

public object Source { get; set; }

There is no explanation nor code sample and since Source is of type object, anything can be assigned. I guess Source is supporting various interfaces as sources, but which ones ?

I know for example that it works with a List<>, which implements a number of interfaces. I guess the most basic of them is IEnumerable<T>. Does Source accept anything that implements IEnumerable<T> and the reason that Source is of type object is because it has to support also IEnumerable ? What else does it support ? Does it take advantage if a higher interface like IList<> is also implemented ?

To all of you who simply cannot resist to mark a question as duplicate:

It's quite frustrating if you mark a question as duplicate and prevent any further answers, just because you have seen somewhere an answer assigning something to Collection​View​Source.Source. There are many of them. But please note that this question does not ask just for one example, but I would like to know everything that can be assigned.

Upvotes: 2

Views: 142

Answers (1)

Maxim
Maxim

Reputation: 2128

From Reference Source:

public static readonly DependencyProperty SourceProperty
            = DependencyProperty.Register(
                    "Source",
                    typeof(object),
                    typeof(CollectionViewSource),
                    new FrameworkPropertyMetadata(
                            (object)null,
                            new PropertyChangedCallback(OnSourceChanged)),
                    new ValidateValueCallback(IsSourceValid));

Let's see how IsSourceValid is implemented:

private static bool IsSourceValid(object o)
{
    return (o == null ||
            o is IEnumerable ||
            o is IListSource ||
            o is DataSourceProvider) &&
            !(o is ICollectionView);
}

Thus, valid types are:

  • IEnumerable
  • IListSource
  • DataSourceProvider

ICollectionView is invalid source.

Upvotes: 5

Related Questions