这个问题需要解决:
有一个数据网格。我把它放进去ObservableCollection<T>。当然你可以把它放在那里ListCollectionView,但这对我没有帮助。因为 :
- 我正在用 .net 4.0 编写,.net 4.0
IsLiveFiltering没有LiveShaping. - 我在我有两个的地方写了我的小
ListCollectionView拐杖ObservableCollection<T>。一个执行一个功能SilentArray-ListCollectionView只是一个容器,另一个执行一个CollectionView突出在 UI 中的功能。我遵循我的更改"SilentArray"并决定如何处理过滤器 - 如果它存在,则将其从视图中删除,保留或添加它(基于它是否适合过滤器)。
现在我面临需要“实时排序”的问题。假设您有一个包含 10 个元素的列表。数字。它们按升序排列。突然间,一个数字发生了变化,当然,上升或下降。目前没有任何事情发生。
但我想了想并决定——你可以进行二分搜索并在该项目下的集合中找到一个位置。它比常规排序更快DataGrid。比较快速排序和二分查找的复杂性。
但问题是我无法DataGrid挖掘(exDataGrid - 扩展)。更新您的视图,它只是没有响应。一段代码——我想到了什么
public class ExDataGrid : DataGrid
{
private readonly object m_sync = new object();
public static readonly DependencyProperty PropertyTypeProperty =
DependencyProperty.Register("ReleavntCollectionView", typeof (RelevantCollectionView), typeof (ExDataGrid),
new FrameworkPropertyMetadata()
{
DefaultValue = null,
});
public RelevantCollectionView ReleavntCollectionView
{
get { return (RelevantCollectionView) GetValue(PropertyTypeProperty); }
set { SetValue(PropertyTypeProperty, value); }
}
protected override void OnSorting(DataGridSortingEventArgs _e)
{
_e.Handled = true;
PrepareSort(_e.Column);
if (ReleavntCollectionView.IsCanSort)
{
SetSortPropertyToColumn(_e.Column);
ReleavntCollectionView.SortByField(_e.Column.SortMemberPath, _e.Column.SortDirection.Value);
}
}
private void PrepareSort(DataGridColumn _sortColumn)
{
if(_sortColumn == null)
throw new ArgumentNullException("_sortColumn");
if(!CanUserSortColumns || !_sortColumn.CanUserSort)
return;
if(!Columns.Any())
return;
foreach (var column in Columns)
{
if(column == _sortColumn)
continue;
column.SortDirection = null;
}
}
private void SetSortPropertyToColumn(DataGridColumn _sortColumn)
{
if(_sortColumn == null)
throw new ArgumentNullException("_sortColumn");
ListSortDirection sortDirection = ListSortDirection.Ascending;
if (_sortColumn.SortDirection.HasValue && _sortColumn.SortDirection == ListSortDirection.Ascending)
{
_sortColumn.SortDirection = ListSortDirection.Descending;
}
else
{
_sortColumn.SortDirection = sortDirection;
}
}
}
找到了我自己的问题的解决方案!需要获取
BindingExpression并更新目标 - 我们的UI Control,即DataGrid,但同时,我们不能忘记 UpdateTarget 将网格中的所有属性重置为默认值,即负责分组和排序的属性,因此您需要记住相对于列的方向,网格是如何排序的。