集合的绑定 [英] Binding of a collection
本文介绍了集合的绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想通过xaml将可观察的集合绑定到网格
集合在xaml1的cs文件中,在xaml2的网格中...我该怎么做..
I want to bind a observable collection to a grid through xaml
collection is in cs file of xaml1 and grid in xaml2... how do i do it..
推荐答案
您需要通过以下对象上的Public属性使ObservableCollection可用xaml1.然后在xaml2中,您需要对xaml1的引用,您可以通过在xaml2的构造函数中传递xaml1的实例来做到这一点.然后,您可以将DataGrid绑定到集合.这是一个简单的示例(我使用了WPF,但是Silverlight的方法相同)
You need to make the ObservableCollection available via a Public Property on xaml1. Then in xaml2 you need a reference to xaml1, you can do this by passing an instance of xaml1 in the constructor of xaml2. Then you can bind the DataGrid to the Collection. Here is a simple example( I used WPF but the methodology is the same for Silverlight)
public partial class MainWindow: Window
{
private ObservableCollection<Person> people;
public ObservableCollection<Person> People
{
get
{
return people;
}
}
public MainWindow()
{
InitializeComponent();
people = new ObservableCollection<Person>() {new Person("a"), new Person("b"), new Person("c"), new Person("d"), new Person("e") };
}
private void buttonClick(object sender, RoutedEventArgs e)
{
Window1 w1 = new Window1(this);
w1.Show();
}
}
public class Person
{
public string Name { get; set; }
public Person(string name)
{
this.Name = name;
}
}
这就是如何在第二个窗口中进行绑定
and this is how to bind in the second window
public partial class Window1 : Window
{
public Window1(MainWindow mw)
{
InitializeComponent();
dataGrid1.DataContext = mw.People;
dataGrid1.DisplayMemberPath = "Name";
}
}
希望对您有帮助
Hope this helps
这篇关于集合的绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文