WPF绑定ItemsSource到静态方法?

时间:2022-07-21 17:02:50

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

我在名为“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.

我想将它直接绑定到ComboBox的ItemsSource属性,但是在XAML而不是代码中,我找不到正确的语法。

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

它应该看起来像我想的那样(EmacGbscCore是包含Article对象的程序集):

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

Thank in advance for your help.

提前感谢您的帮助。

1 个解决方案

#1


22  

You need to declare an ObjectDataProvider in the resources:

您需要在资源中声明ObjectDataProvider:

<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}}"

#1


22  

You need to declare an ObjectDataProvider in the resources:

您需要在资源中声明ObjectDataProvider:

<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}}"