我将ItemsControl绑定到CollectionViewSource。这里是代码: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,我在绑定字段中看到它,但顺序不变。我是否期望不应该发生的事情或者有更好的方法来处理?
请参阅我对[SortDescription和自动排序顺序刷新](http://stackoverflow.com/a/7372522/620360)的回答。 – LPL
WPF 4.5中有一项新的功能叫做实时修整,它解决了这个问题,但如果您不想调用Refresh,您可能必须暂时使用由LPL链接的方法。 – Kris
我能够通过简单地调用暴露视图的属性上的PropertyChanged来解决此问题,让视图刷新(并清除排序),然后添加排序描述。 – PatFromCanada