Intersoft ClientUI Documentation
How-to: Create and Open a UXDialogBox using MVVM Pattern

This example shows how to create and open a UXDialogBox using MVVM pattern.

Example

Description

The UXDialogBox class implements IModalWindow interface which allows you to create an instance of a UXDialogBox from within the ViewModel and reference it as the IModalWindow interface. The interface approach allows the ViewModel to interact with the object without actually knowing the type of the object. You can use the DialogBoxServiceProvider to create the dialog box in the ViewModel.

In addition, ClientUI also provides a number of classes that take advantage of solid design patterns such as provider and shell pattern to interact with the UXDialogBox in the ViewModel. The use of these design patterns are recommended for code efficiency that promotes separation of concerns which is particularly useful for cross-platform development such as in Silverlight and WPF.

The following example shows how to create an instance of a UXDialogBox asynchronously, show the dialog box and handle the callback when the dialog box is closed.

Code

C#
Copy Code
private void EditContact(object parameter)
{
    DialogBoxServiceProvider.GetInstanceAsync(
        DialogBoxServiceProvider.ContactEditView,

        // view result callback
        (dialogBox) =>
        {
            // assign a clone copy of the selected contact to the dialog's data context
            Contact editContact = this.SelectedItem.Contact.Clone();
            dialogBox.DataContext = new ContactEditViewModel(editContact, false);

            // tell view model that we're in editing mode, 
            // so that it can disable/enable related commands as necessary
            this.IsInEditing = true;

            // show the Edit Contact dialog box (view)
            DialogBoxServiceProvider.Show(dialogBox,

                // dialogbox closing callback 
                (dialogResult) =>
                {
                    if (dialogResult == DialogResult.OK)
                    {
                        // user pressed OK, adds the new contact
                        ContactViewModel oldSelection = this.SelectedItem;
                        ContactViewModel newSelection = new ContactViewModel(editContact);
                        int oldSelectionPos = this.Contacts.IndexOf(oldSelection);

                        // clears existing selection
                        this.SelectedItem = null;

                        // apply changes
                        this.Contacts.Remove(oldSelection);
                        this.Contacts.Insert(oldSelectionPos, newSelection);

                        // set the new contact as selection
                        this.SelectedItem = newSelection;
                    }

                    this.IsInEditing = false;
                });
        });
}
See Also

Concepts

Other Resources