我用interited的DataGrid從吉來爲WPF數據網格中的行爲。
該行爲保存了最初的SortDescriptions並將其應用於每次更改ItemsSource
。 你也可以提供一個IEnumerable<SortDescription>
這將導致每一個變化的度假勝地。
行爲
public class DataGridSortBehavior : Behavior<DataGrid>
{
public static readonly DependencyProperty SortDescriptionsProperty = DependencyProperty.Register(
"SortDescriptions",
typeof (IEnumerable<SortDescription>),
typeof (DataGridSortBehavior),
new FrameworkPropertyMetadata(null, SortDescriptionsPropertyChanged));
/// <summary>
/// Storage for initial SortDescriptions
/// </summary>
private IEnumerable<SortDescription> _internalSortDescriptions;
/// <summary>
/// Property for providing a Binding to Custom SortDescriptions
/// </summary>
public IEnumerable<SortDescription> SortDescriptions
{
get { return (IEnumerable<SortDescription>) GetValue(SortDescriptionsProperty); }
set { SetValue(SortDescriptionsProperty, value); }
}
protected override void OnAttached()
{
var dpd = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty, typeof (DataGrid));
if (dpd != null)
{
dpd.AddValueChanged(AssociatedObject, OnItemsSourceChanged);
}
}
protected override void OnDetaching()
{
var dpd = DependencyPropertyDescriptor.FromProperty(ItemsControl.ItemsSourceProperty, typeof (DataGrid));
if (dpd != null)
{
dpd.RemoveValueChanged(AssociatedObject, OnItemsSourceChanged);
}
}
private static void SortDescriptionsPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
if (d is DataGridSortBehavior)
{
((DataGridSortBehavior) d).OnItemsSourceChanged(d, EventArgs.Empty);
}
}
public void OnItemsSourceChanged(object sender, EventArgs eventArgs)
{
// save description only on first call, SortDescriptions are always empty after ItemsSourceChanged
if (_internalSortDescriptions == null)
{
// save initial sort descriptions
var cv = (AssociatedObject.ItemsSource as ICollectionView);
if (cv != null)
{
_internalSortDescriptions = cv.SortDescriptions.ToList();
}
}
else
{
// do not resort first time - DataGrid works as expected this time
var sort = SortDescriptions ?? _internalSortDescriptions;
if (sort != null)
{
sort = sort.ToList();
var collectionView = AssociatedObject.ItemsSource as ICollectionView;
if (collectionView != null)
{
using (collectionView.DeferRefresh())
{
collectionView.SortDescriptions.Clear();
foreach (var sorter in sort)
{
collectionView.SortDescriptions.Add(sorter);
}
}
}
}
}
}
}
XAML與可選SortDescriptions參數
<DataGrid ItemsSource="{Binding View}" >
<i:Interaction.Behaviors>
<commons:DataGridSortBehavior SortDescriptions="{Binding SortDescriptions}"/>
</i:Interaction.Behaviors>
</DataGrid>
視圖模型ICollectionView設置
View = CollectionViewSource.GetDefaultView(_collection);
View.SortDescriptions.Add(new SortDescription("Sequence", ListSortDirection.Ascending));
可選:視圖模型屬性提供多變SortDescriptions
public IEnumerable<SortDescription> SortDescriptions
{
get
{
return new List<SortDescription> {new SortDescription("Sequence", ListSortDirection.Ascending)};
}
}
你應該添加您的更新作爲一個答案,然後接受它(如果你能) –
YEPP,你說得對。完成! –
類似於http://stackoverflow.com/questions/9560528/issue-sorting-datagrid&http://stackoverflow.com/questions/6176771/wpf-datagrid-icollectionview-sorting-bug –