Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
306 views
in Technique[技术] by (71.8m points)

wpf - C# How to code EnableCollectionSynchronization for Collection of Collections

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);
}

}

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Consider this instead:

// 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...