Skip to main content

WizardService

  • 13 minutes to read

WizardService is an IWizardService implementation that allows you to use the Wizard control in accordance with MVVM.

wizard illust

Note

Before you proceed with the Wizard Service, ensure that you read the following help topic: Services - Getting Started.

To handle clicks on wizard page buttons, implement the following interfaces in ViewModels representing pages, so that the corresponding method is called.

public abstract class WizardViewModel : ISupportWizardCancelCommand {
        //...
        public bool CanCancel {
            get { return GetCanCancel(); }
        }
        public void OnCancel(CancelEventArgs e) {
            if(this.GetService<IMessageBoxService>().
                ShowMessage("Do you want to exit the Wizard?", "Confirmation", MessageButton.YesNo, MessageIcon.Question) == MessageResult.No)
                e.Cancel = true;
        }
        protected virtual bool GetCanCancel() {
            return true;
        }
    }

Another way is to pass the corresponding events to the main ViewModel.

<dxco:Wizard DataContext="{dxmvvm:ViewModelSource local:WizardViewModel}" Parameter="{Binding}" ItemsSource="{Binding Items}">
    <!---->
    <dxmvvm:Interaction.Behaviors>
        <dxmvvm:EventToCommand EventName="Cancel" Command="{Binding CancelCommand}" PassEventArgsToCommand="True"/>
    </dxmvvm:Interaction.Behaviors>
    <!---->
</dxco:Wizard>
public class WizardViewModel {
//...
        public void Cancel(DevExpress.Xpf.Core.CancelRoutedEventArgs ea) {
            if(this.GetService<IMessageBoxService>().
                ShowMessage("Do you want to exit the Wizard?", "Confirmation", MessageButton.YesNo, MessageIcon.Question) == MessageResult.No)
                ea.Cancel = true;
        }
//...
}

The table below lists interfaces, methods, and events corresponding to wizard buttons.

Wizard button Interface Method Event
Next ISupportWizardNextCommand ISupportWizardNextCommand.OnGoForward Wizard.Next
Back ISupportWizardBackCommand ISupportWizardBackCommand.OnGoBack Wizard.Back
Cancel ISupportWizardCancelCommand ISupportWizardCancelCommand.OnCancel Wizard.Cancel
Finish ISupportWizardFinishCommand ISupportWizardFinishCommand.OnFinish Wizard.Finish

To display the desired view in the Wizard control, use the IWizardService.Navigate method.

public class WizardPageViewModel : ISupportWizardNextCommand {
        //...

        public bool CanGoForward {
            get { return true; }
        }
        public void OnGoForward(CancelEventArgs e) {
            GoForward();
        }

        protected void GoForward() {
            this.GetRequiredService<IWizardService>().Navigate("Next Page", Model, this);
        }
    }

This example demonstrates how to use the WizardService. See Implementation Details for more information.

See also:

How to create a Wizard with pages defined in XAML

How to: Create a wizard based on a collection of view models

View Example

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using DevExpress.Mvvm;
using DevExpress.Mvvm.POCO;

namespace VM_DrivenWizard.ViewModels {
    public class CongratulationsPageViewModel : WizardViewModelBase, ISupportWizardFinishCommand {
        protected CongratulationsPageViewModel() {
            ShowBack = true;
            ShowCancel = true;
            ShowFinish = true;
            AllowBack = true;
        }
        public bool CanFinish {
            get { return true; }
        }

        public void OnFinish(CancelEventArgs e) {
            this.GetService<IMessageBoxService>().ShowMessage("Thank you for completing this WPF feature tour!", "WPF Tour", MessageButton.OK, MessageIcon.Exclamation);
        }
        protected override bool GetCanCancel() {
            return false;
        }
    }
}