【问题标题】:WPF ObservableCollection<T> vs BindingList<T>WPF ObservableCollection<T> 与 BindingList<T>
【发布时间】:2011-09-09 09:54:17
【问题描述】:

在我的 WPF 应用程序中,我有一个 XamDataGrid。网格绑定到 ObservableCollection。我需要允许用户通过网格插入新行,但事实证明,为了使“添加新行”行可用,xamDataGrid 的源需要实现 IBindingList。 ObservableCollection 没有实现那个接口。

如果我将源更改为 BindingList,它可以正常工作。但是,根据我阅读本主题的了解,BindingList 确实是 WinForms 的东西,并且在 WPF 中并不完全支持。

如果我将所有 ObservableCollections 都更改为 BindingLists,我会犯错吗?关于如何在将源保持为 ObservableCollection 的同时为我的 xamDataGrid 添加新的行功能,是否有人有任何其他建议?据我了解,有许多不同的网格需要实现 IBindingList 以支持添加新行功能,但我看到的大多数解决方案只是切换到 BindingList。

谢谢。

【问题讨论】:

  • 什么是 XamDatagrid ?你的意思是 WPF 工具包数据网格吗?第三方组件?您能否也发布一些示例代码:)
  • @Bruno 这是 Infragistic 的 Datagrid 版本

标签: c# wpf data-binding observablecollection infragistics


【解决方案1】:

我不熟悉 IBindingList,但我可能会采用编写适配器和/或扩展类的方法,将 ObservableCollection 适配到 IBindingList。这样,您可以保留熟悉的 ObservableCollection 代码(也可以在 Infragistic DataGrid 之外的其他地方使用它)。

【讨论】:

    【解决方案2】:

    我认为无论哪种方式你都不走运。网格不会完全支持 IBindingList,所以你会失去我相信的排序之类的东西。但是 OC 不执行 AddNew 行为。

    我不会使用 IBindingList,我可能只是添加一个按钮以将新项目插入列表,然后设置网格以编辑该项目。

    【讨论】:

      【解决方案3】:

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

      您是否检查过xamGrid 是否支持绑定到ICollectionView 源?如果是这样,您可以使用此接口公开您的数据源并使用BindingListCollectionView 支持它。

      您还可以创建ObservableCollection&lt;T&gt; 的子类并实现 IBindingList 接口:

      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&lt;T&gt; 并实现 INotifyCollectionChanged 接口。

      【讨论】:

        【解决方案4】:
        【解决方案5】:

        如果您可以升级到 NetAdvantage 2011 第 2 卷,则添加新记录将在绑定到 ObservableCollection 时起作用。

        如果您使用的是 NetAdvantage 2011 第 1 卷或更早版本,则 XamDataGrid 在其 CanAddNew 属性返回 true 时也支持 IEditableCollectionView 接口。您可以使用 ListCollectionView 为其提供 ObservableCollection 的实例,然后将 XamDataGrid 绑定到 ListCollectionView。

        您也可以使用之前的建议,即从 ObservableCollection 派生并在派生类上实现 IBindingList。

        【讨论】:

        • 我已经升级到 v11.2 但还是不行。当用 BL 替换 OC 时,它又可以工作了。有什么需要设置的吗?
        • @marek 唯一需要的设置是在 FieldLayoutSettings 上将 AllowAddNew 设置为 True,无论您使用 BindingList 还是 ObservableCollection,都需要此设置。我今天早上用 11.2 测试了这个,AddNewRow 与 ObservableCollection 一起工作。如果你能提供一个样本,我可以看看它在哪里不起作用,我会看看。
        猜你喜欢
        • 2010-10-02
        • 1970-01-01
        • 2011-07-15
        • 1970-01-01
        • 1970-01-01
        • 2011-02-17
        • 1970-01-01
        • 2012-05-28
        • 1970-01-01
        相关资源
        最近更新 更多