【发布时间】:2018-11-13 11:03:18
【问题描述】:
我需要在 ListView 中显示一个集合。为此,我有一个带有一个 ListViewItem 的 CompositeCollection 作为集合的搜索栏。我将它与“n”个项目的列表放在一起。这样做的原因是我需要搜索栏不受排序的影响。问题是,我绑定到 CollectionViewSource 的 CollectionView 会产生 BindingError,我不知道为什么。
我的集合的 XAML 代码:
<Grid.Resources>
<CollectionViewSource x:Key="ElementSource" Source="{Binding ElementsCollectionView}"/>
<CompositeCollection x:Key="CompositeCollection">
<ListViewItem Content="{Binding HeaderRow}"/>
<CollectionContainer Collection="{Binding Source={StaticResource ElementSource}}"/>
</CompositeCollection>
</Grid.Resources>
还有我使用的收藏
private NotifyCollection<DataEntry> _data;
public NotifyCollection<DataEntry> Data
{
get { return this._data; }
set { this.SetProperty(ref this._data, value); }
}
private CollectionView _elementsCollectionView;
public CollectionView ElementsCollectionView
{
get { return this._elementsCollectionView; }
set
{
if (value == null) return;
this._elementsCollectionView = value;
}
}
private CompositeCollection _tableItems;
public CompositeCollection TableItems
{
get { return this._tableItems; }
set { this.SetProperty(ref this._tableItems, value); }
}
private DataEntry _headerRow;
public DataEntry HeaderRow
{
get { return this._headerRow; }
set { this.SetProperty(ref this._headerRow, value); }
}
以及正在初始化的集合:
public void SetCollection()
{
this.TableItems.Add(this.HeaderRow);
this.TableItems.Add(this.ElementsCollectionView);
}
this.TableViewModel.ElementsCollectionView =
(CollectionView)CollectionViewSource.GetDefaultView(this.Data);
如您所见,一切正常。 NotifyCollection 继承自 ObservableCollection 并且应该完成工作。如果我将 HeaderRow 添加到“ElementsCollectionView”,一切正常,除了 HeaderRow 已排序。如果我将 CollectionView 添加到 CollectionViewSource,我会得到 BindingError:
System.Windows.Data Error: 5 : Value produced by BindingExpression is
not valid for target property.;
Value='System.Windows.Data.ListCollectionView'
BindingExpression:Path=ElementsCollectionView;
DataItem='AuftragsverwaltungDataTableViewModel' (HashCode=57170378);
target element is 'CollectionViewSource' (HashCode=52642430); target property is 'Source' (type 'Object')
【问题讨论】:
标签: c# wpf mvvm data-binding collections