WPF的DataGrid排序描述仅适用于静态实例

时间:2017-06-22 02:33:37

标签: c# wpf xaml datagrid observablecollection

我在使用DataGridSortDescriptions项进行排序时遇到问题 我有static ObservableCollection存储了我的数据,在将该集合绑定到DataGrid后,我可以使用SortDescription按升序对项目进行排序。
过了一段时间我需要更改我的代码 - 该集合的static实例不再合理。进行这些更改后,我构建了相同的DataGrid视图,但对项目进行排序不再有效。

我需要在DataGrid上创建一个WPF来存储我的应用程序的历史记录。
为此,我创建了一些用作历史记录的类,然后将这些数据绑定到我的DataGrid。因为那些是历史记录,我希望它们以升序显示给用户。这意味着我添加的最后一项将显示在最顶部。

这是我的HistoryRecord课程:

public class HistoryRecord {
    public TransactionSender Sender { get; set; }
    public string Description { get; set; }
    public DateTime Timestamp { get; set; }
}

我创建了一个带有可观察集合的History类,如下所示:

public static class History {
    public static ObservableCollection<HistoryRecord> UserHistory { get; set; } =
        new ObservableCollection<HistoryRecord>();
}

我选择使用可观察的集合,因为这样我就可以更轻松地更新DataGrid

这是我的历史记录标签中xaml的{​​{1}}代码:

DataGrid

这是历史记录标签的<DataGrid x:Name="HistoryGrid" HorizontalAlignment="Left" AutoGenerateColumns="False" IsReadOnly="True" > <DataGrid.Columns> <DataGridTextColumn Header="Sender" Binding="{Binding Sender}"/> <DataGridTextColumn Header="Description" Binding="{Binding Description}"/> <DataGridTextColumn Header="Timestamp" Binding="{Binding Timestamp}"/> </DataGrid.Columns> </DataGrid>

C# code

上面的代码工作得很好。它会对我的物品进行升序排序 上周我需要稍微修改一下我的代码 到目前为止,有一个静态public partial class HistoryTab : UserControl { public HistoryTab() { InitializeComponent(); HistoryGrid.ItemsSource = History.UserHistory; HistoryGrid.Items.SortDescriptions.Add(new SortDescription("Timestamp", ListSortDirection.Ascending)); HistoryGrid.Items.Refresh(); } } 类是好的,但现在我需要为几个事情创建该History类的多个实例。

话虽如此,整个应用程序仍然具有统一的历史记录 为了保存统一的历史记录,我创建了一个名为History的新类:

HistoryUtil

public static class HistoryUtil { public static HistoryStorage AppHistory = new HistoryStorage(); } 类现在称为History,它包含可观察的集合,如前所述:

HistoryStorage

之前的public class HistoryStorage { public ObservableCollection<HistoryRecord> UserHistory { get; set; } = new ObservableCollection<HistoryRecord>(); } 代码保持不变。
我更新了标签的xaml,如下所示:

C# code

此更新的代码不对项目进行排序 此代码显示项目但没有时间戳的升序 我无法根据时间戳弄清楚视图是如何以及为什么没有按升序排序的 这基本上是相同的代码,结构只有一些变化,而且我已将public partial class HistoryTab : UserControl { public HistoryTab() { InitializeComponent(); HistoryGrid.ItemsSource = HistoryUtil.AppHistory.UserHistory; HistoryGrid.Items.SortDescriptions.Add(new SortDescription("Timestamp", ListSortDirection.Ascending)); HistoryGrid.Items.Refresh(); } } 实例更改为static

以下是一些演示此问题的示例数据:

non-static

HistoryRecord action1 = new HistoryRecord() { Sender = TransactionSender.User, Timestamp = DateTime.Now, Description="First record, should appear last" }; action1.StoreRecord(); //After few seconds... HistoryRecord action2 = new HistoryRecord() { Sender = TransactionSender.User, Timestamp = DateTime.Now, Description="Second record, should appear first" }; action2.StoreRecord(); 方法设置如下:

StoreRecord

这是历史数据网格:
History DataGrid

请注意,这不是关于升序或降序的 - 我试过了两个 它似乎不再对物品进行分类。
我在//For the static class: public void StoreRecord() { History.UserHistory.Add(this); } //For the non-static class: public void StoreRecord() { HistoryUtil.AppHistory.UserHistory.Add(this); } 添加了一个断点,它似乎没有到达那里。

感谢您帮助解决问题。

0 个答案:

没有答案