【发布时间】:2020-03-04 16:35:26
【问题描述】:
我正在阅读此内容:https://social.technet.microsoft.com/wiki/contents/articles/26673.wpf-collectionview-tips.aspx 并尝试将其应用于我的代码但没有成功。
PendingTrucks = (CollectionView)new CollectionViewSource { Source = TruckLog }.View;
ParkedTrucks = (CollectionView)new CollectionViewSource { Source = TruckLog }.View;
PendingTrucks.Filter += PendingTrucks_Filter;
ParkedTrucks.Filter += PendingTrucks_Filter;
public static ICollectionView PendingTrucks
{
get; set;
}
public static ICollectionView ParkedTrucks
{
get; set;
}
static bool PendingTrucks_Filter (object value)
{
if (value is Truck truck)
{
return truck.ParkItem is null;
}
// Fallbackvalue
return true;
}
static bool ParkedTrucks_Filter (object value)
{
if (value is Truck truck)
{
return truck.status == 2;
}
// Fallbackvalue
return true;
}
我收到以下错误:NullReferenceException: Object reference not set to an instance of an object.。
当它尝试加载带有绑定到视图的数据网格的表单时显示错误。
这是我得到的调用堆栈:
This exception was originally thrown at this call stack:
System.Windows.Data.ListCollectionView.CanAddNew.get()
System.Windows.Controls.ItemCollection.System.ComponentModel.IEditableCollectionView.CanAddNew.get()
System.Windows.Controls.DataGrid.OnCoerceCanUserAddOrDeleteRows(System.Windows.Controls.DataGrid, bool, bool)
System.Windows.Controls.DataGrid.OnCoerceCanUserAddRows(System.Windows.DependencyObject, object)
System.Windows.DependencyObject.ProcessCoerceValue(System.Windows.DependencyProperty, System.Windows.PropertyMetadata, ref System.Windows.EntryIndex, ref int, ref System.Windows.EffectiveValueEntry, ref System.Windows.EffectiveValueEntry, ref object, object, object, System.Windows.CoerceValueCallback, bool, bool, bool)
System.Windows.DependencyObject.UpdateEffectiveValue(System.Windows.EntryIndex, System.Windows.DependencyProperty, System.Windows.PropertyMetadata, System.Windows.EffectiveValueEntry, ref System.Windows.EffectiveValueEntry, bool, bool, System.Windows.OperationType)
System.Windows.DependencyObject.CoerceValue(System.Windows.DependencyProperty)
System.Windows.Controls.DataGrid.OnItemsSourceChanged(System.Collections.IEnumerable, System.Collections.IEnumerable)
System.Windows.Controls.ItemsControl.OnItemsSourceChanged(System.Windows.DependencyObject, System.Windows.DependencyPropertyChangedEventArgs)
System.Windows.DependencyObject.OnPropertyChanged(System.Windows.DependencyPropertyChangedEventArgs)
...
[Call Stack Truncated]
【问题讨论】:
标签: wpf collectionviewsource icollectionview