将不同的集合绑定到datagrid [英] Binding different collections to datagrid

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

问题描述

我有一些收藏.例如,List 1List 2.两者都是List<Object>.

I have some collections. For example, List 1 and List 2. Both are List<Object>.

我需要做什么:

1)将它们插入Datagrid:

2)为Lists添加新项目.例如,表单上有一些按钮.我单击它,新项目添加到第一个列表. Datagrid现在看起来像这样:

2) Add new items for Lists. For example, there is some button on form. I click it and new item is adding to first list. Datagrid now look like this:

3)以某种方式.当我想将Datagrid的内容传递给我的类对象时,程序必须知道List 1现在包含2个项目,但List 2-1个项目.

3) In some way. When I want to pass content of Datagrid to my class object, program must know that List 1 now contain 2 items, but List 2 - 1 item.

如何最好地执行这些功能?

How can I in best way perform such features?

推荐答案

以下是示例...

public class ViewModel : INotifyPropertyChanged
{
    public ObservableCollection<SomeItem> VmList { get; set; }
    List<SomeItem> List1 = new List<SomeItem>(); 
    List<SomeItem> List2 = new List<SomeItem>(); 
    public ViewModel()
    {
        // VmList is the item source for the grid
        VmList = new ObservableCollection<SomeItem>();
        // create two lists
        for (int i = 0; i < 10; i++)
        {
            List1.Add(new SomeItem{ID = "1", Name = "Name " + i});
        }
        for (int i = 0; i < 10; i++)
        {
            List1.Add(new SomeItem { ID = "2", Name = "Name (2) " + i });
        }
        // merge the two separate lists
        VmList.AddRange(List1);
        VmList.AddRange(List2);
        // get the view
        var lcv = CollectionViewSource.GetDefaultView(VmList);
        // apply a filter
        lcv.Filter = o =>
            {
                var someItem = o as SomeItem;
                if (someItem != null)
                {
                    return someItem.ID == "2";
                }
                return false;
            };
    }
    #region INotifyPropertyChanged Implementation
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string name)
    {
        var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion
}
public class SomeItem:INotifyPropertyChanged
{
    private string _id;
    public string ID
    {
        [DebuggerStepThrough]
        get { return _id; }
        [DebuggerStepThrough]
        set
        {
            if (value != _id)
            {
                _id = value;
                OnPropertyChanged("ID");
            }
        }
    }
    private string _name;
    public string Name
    {
        [DebuggerStepThrough]
        get { return _name; }
        [DebuggerStepThrough]
        set
        {
            if (value != _name)
            {
                _name = value;
                OnPropertyChanged("Name");
            }
        }
    }
    #region INotifyPropertyChanged Implementation
    public event PropertyChangedEventHandler PropertyChanged;
    protected virtual void OnPropertyChanged(string name)
    {
        var handler = System.Threading.Interlocked.CompareExchange(ref PropertyChanged, null, null);
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }
    #endregion
}
public static class Extensions
{
    public static void AddRange<T>(this ObservableCollection<T> collection, IEnumerable<T> list)
    {
        foreach (T t in list)
        {
            collection.Add(t);
        }
    }
}

在此示例(人为设计)中,View Model构造函数创建了两个列表,并将它们添加到绑定到数据网格的可观察集合中.

In this example (which is contrived), the View Model constructor creates two lists and adds them to the observable collection which is bound to the data grid.

然后检索基础集合视图源,并为其附加一个过滤器.

The underlying collection view source is then retrieved and a filter is attached to it.

在您的应用程序中,过滤器将应用于按钮事件处理程序中,而不是Vm构造函数中.这只是一个说明其工作方式的示例.在我的原始评论中,我指出您也可以使用LINQ zip运算符,但是我加入了一个扩展方法,该方法目前可能更有价值.它称为"AddRange".

In your application, the filter would be applied in a button event handler instead of the Vm constructor. This is just a sample to explain how it works. In my original comment, I noted that you could also use a LINQ zip operator, but instead I included an extension method which is probably more valuable at the moment. It's called "AddRange".

这种方法将使您可以将两个列表显示为单个集合,同时在后台保持它们各自的身份.它还显示了如何使用过滤器.

This approach will allow you to present two lists as a single collection while maintaining their separate identities behind the scenes. It also shows how to use a filter.

集合视图"源的文档在此处 http: //msdn.microsoft.com/zh-CN/library/System.Windows.Data.CollectionViewSource.aspx

The docs for Collection View Source are here http://msdn.microsoft.com/en-us/library/System.Windows.Data.CollectionViewSource.aspx

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

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