如果源被新集合替换,则WPF DataGrid排序不执行

时间:2015-03-20 16:11:31

标签: wpf sorting datagrid

我有一个对象列表,每个对象都包含一个集合。我需要从列表中选择一个对象并显示该集合。该集合应按某些属性进行排序。

所以,我有一个ComboBox来显示对象列表。并且DataGrid用于显示所选对象的集合。我使用CollectionViewSource对DataGrid进行排序。它工作......但只是第一次。从列表中选择其他对象后,不再对DataGrid进行排序。

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:componentModel="clr-namespace:System.ComponentModel;assembly=WindowsBase"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <ResourceDictionary>
        <CollectionViewSource x:Key="SortedValues" 
                              Source="{Binding SelectedCategory.Values}">
            <CollectionViewSource.SortDescriptions>
                <componentModel:SortDescription PropertyName="Value" Direction="Ascending" />
            </CollectionViewSource.SortDescriptions>
        </CollectionViewSource>
    </ResourceDictionary>
</Window.Resources>
<Grid>
    <StackPanel>
        <ComboBox ItemsSource="{Binding Path=Categories}"
                  DisplayMemberPath="Name"
                  SelectedItem="{Binding Path=SelectedCategory}"/>
        <DataGrid ItemsSource="{Binding Source={StaticResource SortedValues}}"
                  CanUserSortColumns="False">
        </DataGrid>
    </StackPanel>
</Grid>

代码背后:

using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows;
namespace WpfApplication1
{
public partial class MainWindow : Window, INotifyPropertyChanged
{
    public MainWindow()
    {
        InitializeComponent();

        DataContext = this;

        Categories = new ObservableCollection<MyCategory>
        {
            new MyCategory
            {
                Name = "Cat1",
                Values = new ObservableCollection<MyValue>
                {
                    new MyValue {Value = 5},
                    new MyValue {Value = 4},
                    new MyValue {Value = 3},
                    new MyValue {Value = 2},
                    new MyValue {Value = 1},
                }
            },

            new MyCategory
            {
                Name = "Cat2",
                Values = new ObservableCollection<MyValue>
                {
                    new MyValue {Value = 15},
                    new MyValue {Value = 14},
                    new MyValue {Value = 13},
                    new MyValue {Value = 12},
                    new MyValue {Value = 11},
                }
            }
        };
    }

    public ObservableCollection<MyCategory> Categories { get; set; }

    public MyCategory SelectedCategory
    {
        get
        {
            return _selectedCategory;
        }
        set
        {
            _selectedCategory = value;
            OnPropertyChanged();
        }
    }

    private MyCategory _selectedCategory;


    #region INotifyPropertyChanged

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null) handler(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

public class MyCategory
{
    public string Name { get; set; }
    public ObservableCollection<MyValue> Values { get; set; }
}

public class MyValue
{
    public double Value { get; set; }
}
}

0 个答案:

没有答案