WPF 绑定不通知更改

发布于 2024-07-22 16:02:34 字数 1103 浏览 9 评论 0原文

我有 WPF 排序/绑定问题。 (免责声明:我对 WPF 和数据绑定非常陌生,所以如果我问了一个非常愚蠢的问题,请道歉:-))

首先,我有一个带有 EntitySet的 linqToSql 实体类 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

め七分饶幸 2024-07-29 16:02:34

您应该绑定到 CollectionView 而不是集合本身。 这将允许您指定您需要的任何排序标准。 例子:

var collectionView = new ListCollectionView(contact.Bookings);
collectionView.SortDescriptions.Add(new SortDescription("TravelDate", ListSortDirection.Ascending));
var binding = new Binding();
binding.Source = collectionView;
bookings.SetBinding(ItemsControl.ItemsSourceProperty, binding);

You should bind to a CollectionView rather than the collection itself. That will allow you to specify whatever sorting criteria you require. Example:

var collectionView = new ListCollectionView(contact.Bookings);
collectionView.SortDescriptions.Add(new SortDescription("TravelDate", ListSortDirection.Ascending));
var binding = new Binding();
binding.Source = collectionView;
bookings.SetBinding(ItemsControl.ItemsSourceProperty, binding);
哽咽笑 2024-07-29 16:02:34

Hainesy,

Booking 对象是否实现 INotifyPropertyChanged 来通知 Comments 属性的更改?

更改时,绑定到 Comments 属性的 TextBox 会自动更新

如果没有,您就不能期望当 Comments使用 ObservableCollection 在这种情况下,您只会在从集合中添加或删除 Booking 对象时通过更改更新视图

-Rajesh

Hainesy,

Does the Booking object implement INotifyPropertyChanged to notify change in Comments property?

If not, you cannot expect TextBox which is bound to Comments property to be updated automatically when Comments change

Using ObservableCollection in this case will only get you the benefit of updating the view with changes when Booking objects are added or deleted from the collection

-Rajesh

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文