1

I have a datagrid that correctly sorts when initially loaded. But it doesn't update when I add an item. How can I have the grid update and sort when a new item is added?

<!-- This works for the initial sort, but when members get added to the collection
     the sort doesn't get updated. That's because CollectionViewSource doesn't 
     implement INotifyPropertyChanged. -->
<UserControl.Resources>
    <CollectionViewSource x:Key="SortedApplications" Source="{Binding Applications}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="Name" Direction="Ascending"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
</UserControl.Resources>

One option is to sort the collection within the view model. But if I'm displaying all of the Bars within a Foo, I would then have to break Bars into its own property (in the view model) just so I could sort them.

If possible, I would like to do this without using the view's code-behind because I'm trying not to put code there.

4

1 回答 1

0

默认情况下,DataGrid 支持排序,而无需指定 SortDescription。事实上,我认为您看到的问题是您的 SortDescription 覆盖了 DataGrid 的排序。我会推荐两件事之一:

  1. 删除 SortDescription 并让 DataGrid 处理排序
  2. 将 CollectionViewSource 移动到 ViewModel 并将排序处理程序添加到 DataGrid,该处理程序调用 ViewModel 并管理 SortDescriptions。

如果您计划自己管理 SortDescriptions,请记住在添加新集合之前清除 SortDescriptions 集合,以便排序顺序正确。

更新:

如果在将新项目添加到绑定到的集合中时 DataGrid 未更新,则您的集合不会引发 CollectionChanged 事件。确保你绑定到了实现 INofityCollectionChanged 的​​东西——比如 ObservableCollection。

更新: 这是一个按排序顺序插入项目的示例。您的代码有何不同?

XAML:

<Window x:Class="WpfApplication1.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" Title="MainWindow">
<Window.Resources>
    <CollectionViewSource x:Key="SortedApplications" Source="{Binding Items}">
        <CollectionViewSource.SortDescriptions>
            <scm:SortDescription PropertyName="Age" Direction="Ascending"/>
        </CollectionViewSource.SortDescriptions>
    </CollectionViewSource>
</Window.Resources>

<StackPanel>
    <Button Content="Add Item" Click="AddItem_"/>
    <DataGrid ItemsSource="{Binding Source={StaticResource SortedApplications}}"/>
</StackPanel>       
</Window>

代码:

public partial class MainWindow : Window
{
    public ObservableCollection<Person> Items { get; set; }

    public MainWindow()
    {
        DataContext = this;
        Items = new ObservableCollection<Person>();
        Items.Add(new Person { Name = "Foo", Age = 1 });
        Items.Add(new Person { Name = "Bar", Age = 3 });
        Items.Add(new Person { Name = "Foo", Age = 31 });
        Items.Add(new Person { Name = "Bar", Age = 42 });
        InitializeComponent();

    }

    private void AddItem_(object sender, RoutedEventArgs e)
    {
        Items.Add(new Person { Name = "test", Age = DateTime.Now.Second});
    }
}


public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
}
于 2012-07-01T02:29:48.423 回答