2
我有一个很好的WPF DataGrid绑定到对象集合。一切工作正常,当任何对象的属性更改网格更新。问题是当更新发生时行不会重新排序,然后排序不再有效。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:我使用DataGrid从CTP 2008年10月,我与.NET 3.5
你能告诉我们如何加载和绑定数据吗?你知道取决于你做的方式,你应该重新应用排序标准吗? –
啊,这是我第一次使用WPF DataGRid,我不知道。我正在使用一个特殊的ObservableCollection进行绑定,它允许来自其他线程的更新。我会尝试使用正常的收集并更新问题,谢谢。 –