Skip to main content
A newer version of this page is available. .

Lesson 6 - Lookup Editors and Master Detail

  • 4 minutes to read

This lesson describes how to populate drop-down menus of lookup editors with specific data field records. The second section describes the similar approach used to implement a more complex task - running detail View grids in Master-Detail mode.

Editor Drop-Downs

In Lesson 4, you used MvvmContext’s API to bind grid controls and editors to specific data fields. Editors in the TransactionEditFormView have drop-downs that should be populated with records of existing accounts and categories. This task is accomplished by a single line of code for each editor, as shown below.


fluent.SetBinding(accountBindingSource,
    abs => abs.DataSource, x => x.LookUpAccounts.Entities);
fluent.SetBinding(categoryBindingSource,
    cbs => cbs.DataSource, x => x.LookUpCategories.Entities);

This is possible because the Scaffolding Wizard has generated two bindable properties for your TransactionViewModel:


public IEntitiesViewModel<Account> LookUpAccounts {
    get { return GetLookUpEntitiesViewModel((TransactionViewModel x) => x.LookUpAccounts, x => x.Accounts); }
}

public IEntitiesViewModel<Category> LookUpCategories {
    get { return GetLookUpEntitiesViewModel((TransactionViewModel x) => x.LookUpCategories, x => x.Categories); }
}

These properties retrieve available account and category list, that can be later shown in your TransactionEditFormView. Hence, the majority of the task is already done by the Wizard. However, there can be tasks or certain scenarios, where you will have to manually implement such properties. For instance, applying Master-Detail mode to your grid controls.

Master-Detail Mode

In this Mvvm Expenses sample application, Account and Category Views display lists of available financial accounts and possible transaction categories respectively. It would be reasonable to display all transactions for each account and each existing category type.

  1. Start with adding a new GridControl to both AccountEditFormView and CategoriesEditFormView Views.

    WinForms MVVM - MasterDetailLayout

  2. There are no auto-generated properties by the Scaffolding Wizard, containing transaction lists for Account and Category ViewModels, so implement them manually as the code below illustrates.

    
    //AccountViewModel.cs
    public CollectionViewModel<Transaction, long, IMyDbContextUnitOfWork> AccountTransactionDetails {
        get { return GetDetailsCollectionViewModel((AccountViewModel x) => x.AccountTransactionDetails, x => x.Transactions, x => x.AccountID, (x, key) => x.AccountID = key); }
    }
    
    //CategoryViewModel.cs
    public CollectionViewModel<Transaction, long, IMyDbContextUnitOfWork> CategoryTransactionDetails {
        get { return GetDetailsCollectionViewModel((CategoryViewModel x) => x.CategoryTransactionDetails, x => x.Transactions, x => x.CategoryID, (x, key) => x.CategoryID = key); }
    }
    

    This code will retrieve the Entities collection records from the Transaction model that match this account’s or category’s ID.

  3. Now you can bind your grid controls to these bindable properties.

    
    //AccountEditFormView.cs
    fluent.SetBinding(
        gridControl1, gc => gc.DataSource, x => x.AccountTransactionDetails.Entities);
    
    //CategoriesEditFormView.cs
    fluent.SetBinding(
        gridControl1, gc => gc.DataSource, x => x.CategoryTransactionDetails.Entities);
    

    Additionally, you can switch your grid controls to read-only mode and hide the ‘Account’ column for the AccountEditFormView and ‘Category’ column for the CategoriesEditFormView.

    
    ((GridView)gridControl1.MainView).OptionsBehavior.Editable = false;
    //AccountEditFormView.cs
    ((GridView)gridControl1.MainView).Columns["Account"].Visible = false;
    //CategoriesEditFormView.cs
    ((GridView)gridControl1.MainView).Columns["Category"].Visible = false;
    

    This will make your detail Views show grid controls, running in master-detail mode. The following figure illustrates the result with the horizontal document group within the document manager’s content area. The transaction list, displayed in the right group region, reveals three transactions on the salary card account. Same transactions are shown in the master-detail grid of the ‘Account - Salary Card’ edit View, docked to the group’s left side.

    WinForms MVVM - MasterDetail Applied

See Also