【发布时间】:2011-11-10 10:22:36
【问题描述】:
我有一个很好的 WPF DataGrid 绑定到对象集合。一切正常,当任何对象的属性更改时,网格都会更新。问题是更新发生时行没有重新排序,然后排序不再有效。
知道如何解决这个问题吗?
提前致谢。
编辑:这就是我绑定 DataGrid 的方式:
<Controls:DataGrid MinHeight="300" MinWidth="300" ItemsSource="{Binding Data}" AutoGenerateColumns="True">
public class MainWindowViewModel
{
public ObservableCollectionMultiThread<StockViewModel> Data { get; private set; }
}
public class ObservableCollectionMultiThread<T> : ObservableCollection<T>
{
// Override the event so this class can access it
public override event System.Collections.Specialized.NotifyCollectionChangedEventHandler CollectionChanged;
protected override void OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
// Be nice - use BlockReentrancy like MSDN said
using (base.BlockReentrancy())
{
System.Collections.Specialized.NotifyCollectionChangedEventHandler eventHandler = this.CollectionChanged;
if (eventHandler == null)
return;
Delegate[] delegates = eventHandler.GetInvocationList();
// Walk thru invocation list
foreach (System.Collections.Specialized.NotifyCollectionChangedEventHandler handler in delegates)
{
DispatcherObject dispatcherObject = handler.Target as DispatcherObject;
// If the subscriber is a DispatcherObject and different thread
if (dispatcherObject != null && dispatcherObject.CheckAccess() == false)
{
// Invoke handler in the target dispatcher's thread
dispatcherObject.Dispatcher.Invoke(DispatcherPriority.DataBind, handler, this, e);
}
else // Execute handler as is
handler(this, e);
}
}
}
}
PD:我使用的是 2008 年 10 月 CTP 的 DataGrid,因为我使用的是 Net 3.5
【问题讨论】:
-
你能告诉我们你是如何加载和绑定数据的吗?您知道根据您的操作方式,您应该重新应用排序标准吗?
-
啊,这是我第一次使用 WPF DataGRid,我不知道。我正在使用一个特殊的 ObservableCollection 来绑定,它允许来自其他线程的更新。我将尝试使用普通集合并更新问题,谢谢。
标签: c# .net wpf sorting datagrid