C# 如何为集合的集合编码 EnableCollectionSynchronization

编程入门 行业动态 更新时间:2024-10-28 02:32:01
本文介绍了C# 如何为集合的集合编码 EnableCollectionSynchronization的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我搜索了整个互联网,寻找没有结果的解决方案.在我的程序中,我有 UI 线程,我在其中显示两个数据网格客户和订单.显示选定客户的订单.集合的定义及其更新发生在后台.UI 目的只是为了显示最新信息.我利用了 C# 4.5 中引入的最新功能,即:BindingOperations.EnableCollectionSynchronization 方法.在我的程序中,我有类Customers,它定义了一个Customer 类的集合.依次 Customer 类定义了 Order 类的集合.我的问题是我不知道如何正确编码下面与订单集合绑定同步相关的最后一行代码.此代码经过简化,我省略了 OnPropertyChange 代码行以确保在数据网格中显示正确的属性 - 我想专注于集合.

I have searched the whole internet looking for solution w/o result. In my program, I have UI thread where I display in two datagrids Customers and Orders. Orders are displayed for selected customer. Definition of collections and their update happens in the background. UI purpose is only to display up to date information. I take advantage of the latest functionality introduced in C# 4.5 that is: BindingOperations.EnableCollectionSynchronization method. In my program I have class Customers that defines a collection of Customer class. In turn Customer class defines a collection of Order class. My problem is that I do not know how to properly code the last line of below code which relates to Order collection binding synchronisation. This code is simplified that is I omitted OnPropertyChange code lines to ensure proper properties display in data grid - I want to focus on collections.

public class Customers() { private ObservableCollection<Customer> _customerslist; public ObservableCollection<Customer> customerslist {get { if (_customerslist == null) {_customerslist = new ObservableCollection<Customer>();} return _customerslist; }private set {_customerslist = value;}} } public class Customer { public customerID; public customerName; ..... private ObservableCollection<Order> _orderslist; public ObservableCollection<Order> orderslist {get {if (_orderslist == null) {_orderslist = new ObservableCollection<Order>();} return _orderslist; } private set {_orderslist = value;}} } public class MainWindow : Window { private static object _syncLockCustomers = new object(); private static object _syncLockOrders = new object(); public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { var firstCustomer = Customers.customerslist.FirstOrDefault(); custDataGrid.DataContext = Customers.customerslist; if (firstCustomer != null) { custDataGrid.SelectedItem = firstCustomer; ordersDataGrid.DataContext = firstCustomer.Orders.orderslist; } BindingOperations.EnableCollectionSynchronization(Customers.customerslist, _syncLockCustomers); BindingOperations.EnableCollectionSynchronization(Orders <-what should be here ?, _syncLockOrders); }

}

推荐答案

考虑一下:

// Lock object for synchronization; private readonly object _syncLock = new object(); /// <summary> /// Initializes a new instance of MainWindow. /// </summary> public MainWindow() { InitializeComponent(); // Sync collection with UI BindingOperations.CollectionRegistering += BindingOperations_CollectionRegistering; } /// <summary> /// Handles the CollectionRegistering event of the BindingOperations control. /// </summary> /// <param name="sender">The source of the event.</param> /// <param name="e">The <see cref="CollectionRegisteringEventArgs"/> instance containing the event data.</param> private void BindingOperations_CollectionRegistering(object sender, CollectionRegisteringEventArgs e) { // Ensure collection exists if (Customers == null || Customers.customerslist == null) return; // Ensure collection is synched with UI var myCollection = Customers.customerslist; if (!Equals(e.Collection, myCollection)) return; BindingOperations.EnableCollectionSynchronization(myCollection, _syncLock); }

现在每次有更新时,集合都会正确同步.

Now the collection is properly synched each time there is an update.

更多推荐

C# 如何为集合的集合编码 EnableCollectionSynchronization

本文发布于:2023-11-08 18:51:27,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1570147.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:何为   EnableCollectionSynchronization

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!