C# 如何为集合的集合编码 EnableCollectionSynchronization [英] C# How to code EnableCollectionSynchronization for Collection of Collections

查看:114
本文介绍了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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆