0

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

}

Zibi
  • 3
  • 1
  • 4

2 Answers2

2

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.

Xcalibur37
  • 2,305
  • 1
  • 17
  • 20
0

Move your orders sync object to your Customer class, make it a public field, and enable the synchronization for each customer object you create.

public class Customer
{
    public customerID;
    public customerName;
    public object _syncLockOrders = new object();
    .....
    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 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(firstCustomer.ordersList, firstCustomer._syncLockOrders);
}
Sean Beanland
  • 1,098
  • 1
  • 10
  • 25
  • How can I add BindingOperations.EnableCollectionSynchronization in the non-UI class? It returns an error saying "BindingOperations does not exist in the current context" – Zibi Sep 04 '15 at 17:20
  • The BindingOperations class part of the System.Windows.Data namespace, so you need a reference to PresentationFramework.dll. – Sean Beanland Sep 04 '15 at 17:24
  • It does not seem to work. Having my/previous solution in place, I could at lest see the whole list of customers - now, they are not visible as well. Is there any other solution? I would also prefer to separate UI and operation/data layers. – Zibi Sep 04 '15 at 17:38
  • The problem I see is that if you are trying to use a single static sync lock object for ALL customers, then any background updates to the collection of orders for one customer's orders list could interfere with updates to another customers's orders list, hence why I suggested moving it the Customer class. Perhaps I'm missing something with your simplified example? – Sean Beanland Sep 04 '15 at 17:46
  • I see your point. It makes sense. I sense from your answer that there is no good solution if I use EnableCollectionSynchronization method. What would be an alternative solution, using different method? I hope my problem (display collection of collections) is not so unique that nobody ever dealt with it successfully. – Zibi Sep 04 '15 at 18:09
  • I've updated my answer to have the sync object on the Customer class, but still be able to enable synchronization from the MainWindow class. Would that work for you instead? If I'm missing something still, feel free to clarify. – Sean Beanland Sep 04 '15 at 18:31