2011-06-06 105 views
10

在我的WPF應用程序中,我有一個XamDataGrid。網格綁定到一個ObservableCollection。我需要允許用戶通過網格插入新行,但事實證明,爲了使「添加新行」行可用,xamDataGrid的源需要實現IBindingList。 ObservableCollection不實現該接口。WPF ObservableCollection <T> vs BindingList <T>

如果我將源代碼更改爲BindingList,則它工作正常。然而,從我對這個主題的瞭解我可以理解,BindingList真的是一個WinForms的東西,並沒有完全支持WPF。

如果我將所有ObservableCollections都更改爲BindingLists,我會犯一個錯誤嗎?有沒有人有任何其他的建議,我可以如何爲我的xamDataGrid添加新的行功能,同時保持源爲ObservableCollection?我的理解是,有許多不同的網格要求實現IBindingList以支持添加新行功能,但我所看到的大多數解決方案都只是切換到BindingList。

謝謝。

+1

什麼是XamDatagrid首先?你的意思是WPF工具箱datagrid?第三方組件?你能不能也請張貼一些示例代碼:) – Bruno 2011-06-06 16:01:23

+1

@Bruno它是Infragistic的Datagrid版本 – Rachel 2011-06-06 16:11:31

回答

3

IBindingList接口和BindingList類位於System.ComponentModel命名空間中定義,所以不嚴格有關Windows窗體。

您是否檢查過xamGrid是否支持綁定到ICollectionView源?如果是這樣,您可以使用此界面公開您的數據源並使用BindingListCollectionView備份它。

您還可以創建的ObservableCollection<T>一個子類,並實現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<T>並實現INotifyCollectionChanged接口。

0

我不熟悉IBindingList,但我可能會採取編寫適配器和/或擴展類的方法,使ObservableCollection適應IBindingList。這樣,您可以保留您熟悉的ObservableCollection代碼(也可以在Infragistic DataGrid之外的其他位置使用它)。

0

我覺得你的運氣不好。 IBindingList不會完全被網格支持,所以你會失去像排序我相信的東西。但OC不會執行AddNew行爲。

我不會使用IBindingList,我可能只是添加一個按鈕來插入一個新項目到列表中,然後設置網格來編輯該項目。

0

如果你可以升級到2011年的NetAdvantage第2卷中添加新記錄時綁定到的ObservableCollection會工作。

如果您使用的是NetAdvantage 2011 Volume 1或更早版本,那麼當其CanAddNew屬性返回true時,XamDataGrid也支持IEditableCollectionView接口。您可以使用ListCollectionView爲其提供ObservableCollection的實例,然後將XamDataGrid綁定到ListCollectionView。

您也可以使用以前的建議從派生類派生ObservableCollection和實現IBindingList。

+0

我已升級到v11.2,但它仍然無法正常工作。當用BL替換OC時,它再次起作用。有什麼設置我必須設置? – Marek 2012-11-26 09:53:00

+0

@marek無論您使用BindingList 還是ObservableCollection ,只需要在FieldLayoutSettings上設置爲AllowAddNew的唯一設置。我今天早上用11.2測試了這個,AddNewRow用ObservableCollection工作。如果你可以提供一個樣本,我可以看看它不工作的地方,我會看看。 – alhalama 2012-11-29 13:01:49

相關問題