首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >DataGrid ListCollectionView排序

DataGrid ListCollectionView排序
EN

Stack Overflow用户
提问于 2019-04-20 22:39:54
回答 1查看 324关注 0票数 0

我有一个视图模型,它公开了一个ListCollectionView和绑定到它的数据网格。由于某些原因,当交换ListCollectionView并从源集合创建新项时,添加到源集合中的新项的排序会丢失。

源集合是一个ObservableCollection。在创建ListCollectionView时,源集合中已存在的项的排序是正确的。我不使用GetDefaultView,而是在需要的时候自己创建ListCollectionView。向源集合添加新项时,不会进行排序,并且项将显示在列表的末尾。在交换ListCollectionView时引发INotifyPropertyChanged。

有人知道我为什么会有这样的行为吗?

EN

回答 1

Stack Overflow用户

发布于 2019-04-21 22:14:35

问题可能出在你创建列表集合视图的方式上。(您应该发布该代码。)

我把一些实验性的代码放在一起,它的工作与我预期的一样。我只使用了列表框,但这不会有什么不同。

代码语言:javascript
复制
<Window.DataContext>
    <local:MainWindowViewModel/>
</Window.DataContext>
<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="*"/>
        <ColumnDefinition Width="100"/>
    </Grid.ColumnDefinitions>
    <Button Command="{Binding AddPersonCommand}" Grid.Column="1"/>
    <ListBox ItemsSource="{Binding People}"
             >
        <ListBox.ItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding LastName}"/>
            </DataTemplate>
        </ListBox.ItemTemplate>
    </ListBox>
</Grid>

视图模型

代码语言:javascript
复制
public class MainWindowViewModel : BaseViewModel
{
    private RelayCommand addPersonCommand;
    public RelayCommand AddPersonCommand
    {
        get
        {
            return addPersonCommand
            ?? (addPersonCommand = new RelayCommand(
              () =>
             {
                 People.Add(new Person { FirstName = "Adam", LastName = "Barlow" });
             }
             ));
        }
    }
    private ObservableCollection<Person> people = new ObservableCollection<Person>();

    public ObservableCollection<Person> People
    {
        get { return people; }
        set { people = value; }
    }


    public ListCollectionView LCV { get; set; }
    public MainWindowViewModel()
    {
        LCV = (ListCollectionView)CollectionViewSource.GetDefaultView(People);
        LCV.SortDescriptions.Add(
            new SortDescription("LastName", ListSortDirection.Ascending));
        People.Add(new Person { FirstName = "Chesney", LastName = "Brown" });
        People.Add(new Person { FirstName = "Gary", LastName = "Windass" });
        People.Add(new Person { FirstName = "Liz", LastName = "McDonald" });
        People.Add(new Person { FirstName = "Carla", LastName = "Connor" });
    }
}

当我单击按钮时,它会将Ken Barlow添加到可观察的集合中,并且它会出现在列表框的顶部。

我还尝试了绑定到LCV:

并通过ctor实例化传入的人。

代码语言:javascript
复制
public class MainWindowViewModel : BaseViewModel
{
    private RelayCommand addPersonCommand;
    public RelayCommand AddPersonCommand
    {
        get
        {
            return addPersonCommand
            ?? (addPersonCommand = new RelayCommand(
              () =>
              {
                  Person person = new Person { FirstName = "Adam", LastName = "Barlow" };
                  People.Add(person);
              }
             ));
        }
    }

    private ObservableCollection<Person> people = new ObservableCollection<Person>();

    public ObservableCollection<Person> People
    {
        get { return people; }
        set { people = value; }
    }

    public ListCollectionView LCV { get; set; }
    public MainWindowViewModel()
    {
        People.Add(new Person { FirstName = "Chesney", LastName = "Brown" });
        People.Add(new Person { FirstName = "Gary", LastName = "Windass" });
        People.Add(new Person { FirstName = "Liz", LastName = "McDonald" });
        People.Add(new Person { FirstName = "Carla", LastName = "Connor" });
        LCV = new ListCollectionView(People);
        LCV.SortDescriptions.Add(
            new SortDescription("LastName", ListSortDirection.Ascending));
    }
}

这也是可行的。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55774525

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档