Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WPF Binding ItemsSource to a static method?

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.

like image 655
Karnalta Avatar asked May 12 '11 08:05

Karnalta


1 Answers

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}}"
like image 146
Thomas Levesque Avatar answered Nov 15 '22 22:11

Thomas Levesque