WPF 绑定不通知更改
我有 WPF 排序/绑定问题。 (免责声明:我对 WPF 和数据绑定非常陌生,所以如果我问了一个非常愚蠢的问题,请道歉:-))
首先,我有一个带有 EntitySet
属性预订就可以了。Contact
。
如果我直接将此 Bookings 属性绑定到 ListView
,应用程序似乎会正确通知 ListView
中所选项目的更改,这样带有 { 的文本框Binding Path=Bookings/Comments}
正确更新。
// This code works, but Bookings is unsorted
var binding = new Binding();
binding.Source = contact.Bookings;
bookings.SetBinding(ItemsControl.ItemsSourceProperty, binding);
但是,因为我似乎无法找到对 EntitySet 进行排序的方法(请参阅 这篇文章),我试图绑定到一个可观察集合,例如:
// This code doesn't notify of selected item changes in the ListView
var binding = new Binding();
binding.Source = new ObservableCollection<Booking>(contact.Bookings.OrderByDescending(b => b.TravelDate).ToList());
bookings.SetBinding(ItemsControl.ItemsSourceProperty, binding);
但这似乎没有正确通知评论文本框以使其更新。
如果有人有一个在绑定之前或之后对数据进行排序的解决方案,或者另一个可行的解决方案,我们将不胜感激。
I have a WPF sorting/binding issue. (Disclaimer: I am very new to WPF and databinding so apologise if I am asking a really dumb question :-))
Firstly, I have a linqToSql entity class Contact
with an EntitySet<Booking>
property Bookings on it.
If I directly bind this Bookings property to a ListView
, the application seems to correctly notify of changes to the selected item in the ListView
, such that a textbox with {Binding Path=Bookings/Comments}
updates correctly.
// This code works, but Bookings is unsorted
var binding = new Binding();
binding.Source = contact.Bookings;
bookings.SetBinding(ItemsControl.ItemsSourceProperty, binding);
However, as I can't seem to be able to find a way to sort an EntitySet (see this post), I am trying to bind instead to an Observable collection, e.g:
// This code doesn't notify of selected item changes in the ListView
var binding = new Binding();
binding.Source = new ObservableCollection<Booking>(contact.Bookings.OrderByDescending(b => b.TravelDate).ToList());
bookings.SetBinding(ItemsControl.ItemsSourceProperty, binding);
But this doesn't seem to notify the comments textbox correctly such that it updates.
If anyone has a solution for either sorting the data before or after its bound, or another solution that will work that would be much appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您应该绑定到
CollectionView
而不是集合本身。 这将允许您指定您需要的任何排序标准。 例子:You should bind to a
CollectionView
rather than the collection itself. That will allow you to specify whatever sorting criteria you require. Example:Hainesy,
Booking
对象是否实现INotifyPropertyChanged
来通知Comments
属性的更改?更改时,绑定到
Comments
属性的TextBox
会自动更新如果没有,您就不能期望当
Comments
使用ObservableCollection
在这种情况下,您只会在从集合中添加或删除Booking
对象时通过更改更新视图-Rajesh
Hainesy,
Does the
Booking
object implementINotifyPropertyChanged
to notify change inComments
property?If not, you cannot expect
TextBox
which is bound toComments
property to be updated automatically whenComments
changeUsing
ObservableCollection
in this case will only get you the benefit of updating the view with changes whenBooking
objects are added or deleted from the collection-Rajesh