简体   繁体   English

在另一个项目中的WPF XAML绑定

[英]WPF XAML Binding in another project

How can i bind a listbox for exemple to a method which is in another project like this : 我如何将一个列表框绑定到另一个项目中的方法中,例如:

Project1(BDD) Class1 Project2(GUI) class2 Project1(BDD)类别1 Project2(GUI)类别2

I want to bind a listbox itemSource in the second project(GUI), with a class created in my first project(BDD). 我想将第二个项目(GUI)中的列表框itemSource与在我的第一个项目(BDD)中创建的类绑定在一起。

How can i bind a listbox for exemple to a method which is in another project? 我如何将一个列表框示例绑定到另一个项目中的方法?

Put simply, we don't do that in WPF. 简而言之,我们不在WPF中这样做。 Instead, you should add a reference to the other project, add a using declaration and then instantiate an object from that class. 相反,您应该添加对另一个项目的引用,添加一个using声明,然后从该类实例化一个对象。 Then (assuming that your method returns a collection of some sort) you should set the output of the method to a collection property that you then data bind to the ItemsSource property. 然后(假定您的方法返回某种集合),应将方法的输出设置为collection属性,然后将数据绑定到ItemsSource属性。

Here's a very basic example: 这是一个非常基本的示例:

private ObservableCollection<int> numbers = new ObservableCollection<int>();
public ObservableCollection<int> Numbers
{
    get { return numbers; }
    set { numbers = value; NotifyPropertyChanged("Numbers"); }
}

... ...

Numbers = new YourClassFromOtherProject().GetData();

... ...

<ItemsControl ItemsSource="{Binding Numbers}" ... />

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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