WPF与集合对象的数据绑定 [英] WPF Databinding With A Collection Object

查看:62
本文介绍了WPF与集合对象的数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的类,定义如下:

I have a simple class as defined below:

public class Person
{
    int _id;
    string _name;

    public Person()
    { }

    public int ID
    {
        get { return _id; }
        set { _id = value; }
    }

    public string Name
    {
        get { return _name; }
        set { _name = value; }
    }
}

存储在数据库中,并通过更多代码将其放入ObservableCollection对象中,以尝试稍后在WPF中进行数据绑定:

that is stored in a database, and thru a bit more code I put it into an ObservableCollection object to attempt to databind in WPF later on:

 public class People : ObservableCollection<Person>
{
    public People() : base() { }

    public void Add(List<Person> pListOfPeople)
    {
        foreach (Person p in pListOfPeople) this.Add(p);
    }
}

在XAML中,我有一个ListView,我想为人"目录中的每个项目填充一个ListViewItem(由文本块组成).从数据库更新对象.我还希望该文本块绑定到名称"Person对象的属性.

In XAML, I have myself a ListView that I would like to populate a ListViewItem (consisting of a textblock) for each item in the "People" object as it gets updated from the database. I would also like that textblock to bind to the "Name" property of the Person object.

起初我以为我可以这样做:

I thought at first that I could do this:

lstPeople.DataContext = objPeople;

其中lstPeople是我的XAML中的ListView控件,但是当然不执行任何操作.我在网上找到了大量示例,其中人们通过XAML创建对象,然后通过其XAML绑定到该对象.但是没有一个我们绑定到实例化对象并相应地重新绘制的对象.

where lstPeople is my ListView control in my XAML, but that of course does nothing. I've found TONS of examples online where people through XAML create an object and then bind to it through their XAML; but not one where we bind to an instantiated object and re-draw accordingly.

有人可以给我一些建议吗?

Could someone please give me a few pointers on:

A)如何将ListView控件绑定到我实例化的人物"列表中.收集对象?

A) How to bind a ListView control to my instantiated "People" collection object?

B)如何将模板应用于ListView以便为集合中的对象设置格式?

B) How might I apply a template to my ListView to format it for the objects in the collection?

即使链接到一个不错的示例(请不要在XAML中声明的对象上进行操作).

Even links to a decent example (not one operating on an object declared in XAML please) would be appreciated.

推荐答案

您是如此亲密.

lstPeople.ItemsSource = objPeople; // :)

您唯一需要做的另一件事就是如何为集合中的每个项目应用视图.没问题.我不会使用ListView ...我只会使用ItemsControl,因为它要简单一些.

The only other thing you need is how to apply a view for each item in your collection. No problem. I won't use a ListView... I'll just use an ItemsControl because it's a bit simpler.

<ItemsControl x:Name="lstPeople">
    <ItemsControl.ItemTemplate>
         <DataTemplate>
              <TextBlock Text="{Binding Name}" />
         </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>

就是这样.Listview的策略是相同的,但是您只需要提供更多XAML即可提供列标题和内容.我目前还不确定100%是否需要此功能,因此我将其遗漏了.

That's pretty much it. The strategy is the same for a Listview, but you need to provide just a tad more XAML to provide column headers and stuff. I'm not 100% sure you need this at the moment, so I left it out.

编辑:这是"AddRange"的扩展方法,该方法通过将ObservableCollection子类化来完成您要执行的操作.轻松一点……尤其是如果您最终有很多收集类型的话(会)

Here's an extension method for "AddRange" that will do what you are trying to do by subclassing ObservableCollection. Little easier... especially if you end up with a lot of collection types (you will)

public static void AddRange<T>(this ObservableCollection<T> collection, IEnumerable<T> items)
{
     foreach(var item in items)
     {
          collection.Add(item);
     }
}

然后您可以做:

ObservableCollection<Person> peeps = new ObservableCollection<Person>();
peeps.AddRange(new List<Person>
{ 
     new Person() { Name = "Greg" }, 
     new Person() { Name = "Joe" } 
});

这篇关于WPF与集合对象的数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆