【发布时间】:2012-07-01 12:45:53
【问题描述】:
我正在将 ItemsControl 绑定到 CollectionViewSource。这是代码:
this.Trucks = new ObservableCollection<Truck>();
foreach (var truck in DataRepository.Trucks.Where(t => t.ReadyDate.Date.Equals(this.Date)))
{
this.Trucks.Add(truck);
}
this.TrucksSource = new CollectionViewSource { Source = this.Trucks };
this.TrucksSource.SortDescriptions.Add(new SortDescription("ReadyAddress.Region.RegionNumber", ListSortDirection.Ascending));
this.TrucksSource.SortDescriptions.Add(new SortDescription("TruckId", ListSortDirection.Ascending));
当我最初绑定时 - 排序工作。当我将项目添加到 ObservableCollection 时 - 它被插入到正确的位置,这很好。但是,当我更改排序依据的属性时 - 此项目不会在列表中“移动”。
ReadyAddress.Region.RegionNumber 正确引发 INotifyPropertyChanged,我在绑定字段中看到它,但顺序没有改变。我是否期望某些不应该发生的事情或有更好的方法来处理?
【问题讨论】:
-
WPF 4.5 中有一个新特性叫做 liveShaping,可以解决这个问题,但是如果你不想调用 Refresh,可能暂时只能使用 LPL 链接的方法。跨度>
-
我可以通过简单地在公开视图的属性上调用 PropertyChanged 来解决此问题,让视图刷新(并清除排序),然后添加排序描述。
标签: c# wpf binding mvvm collections