【问题标题】:Sorting an ObservableCollection after editing a property of an item在编辑项目的属性后对 ObservableCollection 进行排序
【发布时间】:2018-09-20 10:51:37
【问题描述】:

我正在尝试将 ObservableCollection<T> 绑定到 WPF 中的 DataGrid。 在DataGrid下方,有一些字段可以编辑DataGrid中当前选择的项目,如下所示:

所以ObservableCollection<T> 的通用T 具有以下属性: - 标题(Überschrift) - 描述(Beschreibung) - 路径(Pfad)

它还有一个属性Reihenfolge,意思是Order

使用黄色箭头,我希望能够修改条目的顺序。

不幸的是,ObservableCollection 没有 OrderBy 方法...


我尝试了以下方法:

在 XAML 中,我定义了一个 CollectionViewSource,如下所示:

<CollectionViewSource Source="{Binding Bilder}" x:Key="Pictures">
    <CollectionViewSource.SortDescriptions>
        <scm:SortDescription PropertyName="Reihenfolge" />
    </CollectionViewSource.SortDescriptions>
</CollectionViewSource>

我已经将DataGrid 绑定到这个CollectionViewSource

<DataGrid Grid.Column="0" Grid.Row="1"
          Name="PictureDataGrid"  
          ItemsSource="{Binding Source={StaticResource Pictures}}"
          AutoGenerateColumns="False" 
          IsReadOnly="True" 
          CanUserAddRows="false" 
          SelectedItem="{Binding SelectedBild}"  
          HorizontalAlignment="Stretch" 
          VerticalAlignment="Stretch">
 ...

在 ViewModel 中,我有以下属性:

public ObservableCollection<BildNotifiableModel> Bilder { get; set; }
public BildNotifiableModel SelectedBild { get; set; }

以及使用DelegateCommands 调用的两个更新订单的方法

 private void MoveSeiteUp()
 {
     const int smallestReihenfolge = 1;
     if (this.SelectedBild.Reihenfolge > smallestReihenfolge) {
            var bildToSwapReihenfolgeWith = this.Bilder.Single(b => b.Reihenfolge == this.SelectedBild.Reihenfolge - 1);
            this.SelectedBild.Reihenfolge--;
            bildToSwapReihenfolgeWith.Reihenfolge++;

            RaisePropertyChanged(nameof(this.Bilder));
        }
    }

    private void MoveSeiteDown()
    {
        if (this.SelectedBild.Reihenfolge < MaxAllowedImages) {
            var bildToSwapReihenfolgeWith = this.Bilder.Single(b => b.Reihenfolge == this.SelectedBild.Reihenfolge + 1);
            this.SelectedBild.Reihenfolge++;
            bildToSwapReihenfolgeWith.Reihenfolge--;

            RaisePropertyChanged(nameof(this.Bilder));
        }
    }

顺序得到正确更新,但不幸的是,视图没有反映更改...只有在关闭并重新打开视图后,DataGrid 中的条目才处于正确的顺序。

  • 我在这里做错了什么?
  • 如何在更改订单时更新 DataGrid?

提前致谢

【问题讨论】:

  • 与其提供所有代码的翻译,不如在这里发布翻译版本?
  • 回答您的问题:check this out
  • ObservableCollection&lt;T&gt; 类有一个 Move 方法,您应该可以在这里使用它。
  • @ChristianMurschall 这有帮助,谢谢!如果您将此添加为答案,我会接受它:)

标签: c# wpf datagrid prism observablecollection


【解决方案1】:

我认为问题在于 CollectionView 没有从其元素中侦听 PropertyChanged-Events,而且 RaisePropertyChanged(nameof(this.Bilder)); 也不起作用,因为 CollectionView 并没有真正改变。

我建议通过CollectionViewSource.GetDefaultView(list) 在代码中创建 CollectionView。因此,您可以从模型中控制 CollectionView 并在需要时调用 ICollectionView.Refresh

【讨论】:

    【解决方案2】:

    在您的方法中,创建一个新集合并将其添加到“图片”。只需提高 PropertyChanged 就会执行引用相等的评估。如果它是相同的 - 如果你只是在里面移动项目 - 它会是这样 - 它不会更新 DataGrid。

    如果您不使用 ObservableCollections 属性(例如自动更新),则在添加或删除项目时,您也可以将其更改为“普通”列表。

    private void MoveSeiteUp()
    {
     const int smallestReihenfolge = 1;
     if (this.SelectedBild.Reihenfolge > smallestReihenfolge) {
            var bildToSwapReihenfolgeWith = this.Bilder.Single(b => b.Reihenfolge == this.SelectedBild.Reihenfolge - 1);
            this.SelectedBild.Reihenfolge--;
            bildToSwapReihenfolgeWith.Reihenfolge++;
    
            this.Bilder = new ObservableCollection<BildNotifiableModel> (this.Bilder);
    
            RaisePropertyChanged(nameof(this.Bilder));
        }
    }
    
    private void MoveSeiteDown()
    {
        if (this.SelectedBild.Reihenfolge < MaxAllowedImages) {
            var bildToSwapReihenfolgeWith = this.Bilder.Single(b => b.Reihenfolge == this.SelectedBild.Reihenfolge + 1);
            this.SelectedBild.Reihenfolge++;
            bildToSwapReihenfolgeWith.Reihenfolge--;
    
            this.Bilder = new ObservableCollection<BildNotifiableModel> (this.Bilder);
    
            RaisePropertyChanged(nameof(this.Bilder));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-02-07
      • 2015-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-07-02
      相关资源
      最近更新 更多