WPF的ObservableCollection< T> VS的BindingList< T> [英] WPF ObservableCollection<T> vs BindingList<T>

查看:141
本文介绍了WPF的ObservableCollection< T> VS的BindingList< T>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的WPF应用程序我有一个XamDataGrid。网格绑定到一个ObservableCollection。我需要让用户通过网格中插入新行,但事实证明,以便在添加新行行可用于中,xamDataGrid源需要实现IBindingList的。的ObservableCollection不实现该接口。

In my WPF app I have a XamDataGrid. The grid is bound to an ObservableCollection. I need to allow users to insert new rows through the grid but it turns out that in order for the "Add New Row" row to be available, the xamDataGrid's source needs to implement IBindingList. ObservableCollection does not implement that interface.

如果我改变我的源到的BindingList,它工作正常。但是,从我可以从这个话题阅读了理解的BindingList真的是一个WinForms的东西,而不是在WPF完全支持。

If I change my source to a BindingList, it works ok. However, from what I can understand from reading up on this topic, BindingList is really a WinForms thing and is not fully supported in WPF.

请问我可以犯了一个错误,如果我改变了我所有的ObservableCollections来BindingLists?没有人有任何其他建议,我怎么能为我的xamDataGrid添加新行功能,同时保持源作为一个ObservableCollection?这是我的理解是有一些需要IBindingList的为了支持添加新行的功能,但大多数解决方案我看到的只是转的BindingList要实施不同的网格。

Would I be making a mistake if I changed all of my ObservableCollections to BindingLists? Does anyone have any other suggestions as to how I can get add new row functionality for my xamDataGrid while keeping the source as an ObservableCollection? It is my understanding that there are a number of different grids that require IBindingList to be implemented in order to support add new row functionality but most solutions I see are to just switch to BindingList.

感谢。

推荐答案

IBindingList的接口的的BindingList 类是在System.ComponentModel命名空间中定义,因此是与不严格的Windows窗体。

The IBindingList interface and BindingList class are defined in the System.ComponentModel namespace, and so are not strictly Windows Forms related.

您是检查的 xamGrid 支持绑定到的的ICollectionView 源?如果是这样,你可以使用这个接口暴露你的数据来源和使用的 BindingListCollectionView

Have you checked is xamGrid supports binding to a ICollectionView source? If so, you could expose your data sources using this interface and back it using a BindingListCollectionView.

您也可以创建一个子类的ObservableCollection< T> 和实现IBindingList的接口:

You could also create a subclass of ObservableCollection<T> and implement the IBindingList interface:

using System;
using System.ComponentModel;
using System.Collections.Generic;
using System.Collections.ObjectModel;

public class ObservableBindingList<T> : ObservableCollection<T>, IBindingList
{
    //  Constructors
    public ObservableBindingList() : base()
    {
    }

    public ObservableBindingList(IEnumerable<T> collection) : base(collection)
    {
    }

    public ObservableBindingList(List<T> list) : base(list)
    {
    }

    //  IBindingList Implementation
    public void AddIndex(PropertyDescriptor property)
    {
        throw new NotImplementedException();
    }

    public object AddNew()
    {
        throw new NotImplementedException();
    }

    public bool AllowEdit
    {
        get { throw new NotImplementedException(); }
    }

    public bool AllowNew
    {
        get { throw new NotImplementedException(); }
    }

    public bool AllowRemove
    {
        get { throw new NotImplementedException(); }
    }

    public void ApplySort(PropertyDescriptor property, ListSortDirection direction)
    {
        throw new NotImplementedException();
    }

    public int Find(PropertyDescriptor property, object key)
    {
        throw new NotImplementedException();
    }

    public bool IsSorted
    {
        get { throw new NotImplementedException(); }
    }

    public event ListChangedEventHandler ListChanged;

    public void RemoveIndex(PropertyDescriptor property)
    {
        throw new NotImplementedException();
    }

    public void RemoveSort()
    {
        throw new NotImplementedException();
    }

    public ListSortDirection SortDirection
    {
        get { throw new NotImplementedException(); }
    }

    public PropertyDescriptor SortProperty
    {
        get { throw new NotImplementedException(); }
    }

    public bool SupportsChangeNotification
    {
        get { throw new NotImplementedException(); }
    }

    public bool SupportsSearching
    {
        get { throw new NotImplementedException(); }
    }

    public bool SupportsSorting
    {
        get { throw new NotImplementedException(); }
    }
}



另外,还可以继承的BindingList< T> 和实施的 INotifyCollectionChanged 接口。

这篇关于WPF的ObservableCollection&LT; T&GT; VS的BindingList&LT; T&GT;的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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