[英]Remember WPF DataGrid sort order
這是這個問題的延續。
我有一個wpf窗口,其中包含帶有動態創建項的TabControl
。 此TabControl
的ItemSource
綁定到Group
的列表。 這些組包含在TabPage
的DataGrid
顯示的元素列表。
XAML:
<Window x:Name="window" x:Class="TestWpf.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:vm="clr-namespace:TestWpf"
Title="MainWindow" Height="350" Width="525">
<TabControl x:Name="tabControl" BorderThickness="0" ItemsSource ="{Binding Groups, ElementName=window, NotifyOnSourceUpdated=True}">
<TabControl.ItemTemplate>
<DataTemplate DataType="{x:Type vm:Group}">
<TextBlock Padding="2" Text="{Binding Name}" HorizontalAlignment="Center" VerticalAlignment="Center"/>
</DataTemplate>
</TabControl.ItemTemplate>
<TabControl.ContentTemplate>
<DataTemplate x:Name="contentTemplate" DataType="{x:Type vm:Group}">
<DataGrid x:Name="dgElements" ItemsSource="{Binding Elements, BindsDirectlyToSource=True}" DockPanel.Dock="Top" AutoGenerateColumns="False" >
<DataGrid.Columns>
<DataGridTextColumn x:Name="clmName" Header="Name" Binding="{Binding Name}" IsReadOnly="True" CanUserReorder="False" />
<DataGridTextColumn x:Name="clmDesc" Header="Description" Binding="{Binding Description}" IsReadOnly="True" CanUserReorder="False" />
</DataGrid.Columns>
</DataGrid>
</DataTemplate>
</TabControl.ContentTemplate>
</TabControl>
</Window>
這一切都很好。
DataGrid
允許開箱即用的多列排序。 但不幸的是,如果我在標簽頁之間切換,排序將會丟失。
我希望為每個tabpage / datagrid維護多列排序 。
可以將DataGridTextColumn
的SortDirection
DataGridTextColumn
到屬性,但是這不記得列的排序順序 。 調試時,我發現當我切換標簽頁時,在設置新的ItemSource
之前 ,將重置SortDirection
,因此無法存儲它。
問題是: 如何保持每個選項卡頁/數據網格的多列排序設置?
在閱讀了很多文章之后,我幾乎找到了解決方案。
我從DataGrid
繼承了自己的類,並將每個ItemSource
的SortDescriptions
存儲在Dictionary
:
public class SortKeepingDataGrid : DataGrid
{
// Dictionary to keep SortDescriptions per ItemSource
private readonly Dictionary<object, List<SortDescription>> m_SortDescriptions =
new Dictionary<object, List<SortDescription>>();
protected override void OnSorting(DataGridSortingEventArgs eventArgs)
{
base.OnSorting(eventArgs);
UpdateSorting();
}
protected override void OnItemsSourceChanged(IEnumerable oldValue, IEnumerable newValue)
{
base.OnItemsSourceChanged(oldValue, newValue);
ICollectionView view = CollectionViewSource.GetDefaultView(newValue);
view.SortDescriptions.Clear();
// reset SortDescriptions for new ItemSource
if (m_SortDescriptions.ContainsKey(newValue))
foreach (SortDescription sortDescription in m_SortDescriptions[newValue])
{
view.SortDescriptions.Add(sortDescription);
// I need to tell the column its SortDirection,
// otherwise it doesn't draw the triangle adornment
DataGridColumn column = Columns.FirstOrDefault(c => c.SortMemberPath == sortDescription.PropertyName);
if (column != null)
column.SortDirection = sortDescription.Direction;
}
}
// Store SortDescriptions in dictionary
private void UpdateSorting()
{
ICollectionView view = CollectionViewSource.GetDefaultView(ItemsSource);
m_SortDescriptions[ItemsSource] = new List<SortDescription>(view.SortDescriptions);
}
}
因此,基本上,每當用戶更改排序時,我都會調用UpdateSorting
並將當前的SortDescription
存儲在每個ItemSource
字典中。
當ItemSource
更改后,我將查找SortDescription
並以正確的順序對其進行重置。
棘手的部分是找到正確的DataGridColumn
來設置其SortDirection
。 這是繪制三角形裝飾所必需的。 我在這里依靠SortMemberPath
和PropertyName
的相等PropertyName
。 最終可能需要一種更通用的方法。
在XAML中,我用SortKeepingDataGrid
替換了DataGrid
,現在按標簽頁存儲了排序。
由於我找不到其他解決方案,因此這可能也會對其他人有所幫助。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.