WPF Binding ItemsSource to a static method?

Karnalta picture Karnalta · May 12, 2011 · Viewed 8.1k times · Source

I have the following static method in an class called "Article" :

public static ObservableCollection<Article> GetObservableCollection() { ... }

And I'd like to bind this directly to the ItemsSource property of a ComboBox but in the XAML not in code, I can't find the right syntax.

It should look something like this I think (EmacGbscCore is the assembly containing Article object) :

ItemsSource="{Binding Source={x:Static EmacGbscCore:Article.GetObservableCollection}}"

Thank in advance for your help.

Answer

Thomas Levesque picture Thomas Levesque · May 12, 2011

You need to declare an ObjectDataProvider in the resources:

<ObjectDataProvider x:Key="data"
                    ObjectType="{x:Type EmacGbscCore:Article}"
                    MethodName="GetObservableCollection" />

And use this as the source of your binding:

ItemsSource"{Binding Source={StaticResource data}}"