This is a migrated thread and some comments may be shown as answers.

Wizard validation VM

4 Answers 118 Views
Wizard
This is a migrated thread and some comments may be shown as answers.
enrico
Top achievements
Rank 1
Veteran
enrico asked on 15 Apr 2020, 10:46 AM
Hi, I'm a new developer in WPF.

I'm looking a way to validate a page of Wizard control when the "next" button is clicked. 
Basically I have a wizard where the first step check a connection with a SQL Database. This check is made from ViewModel. I need that when the Next is pressed the ViewModel do the check and if return "true" the wizard go to next step.

How I can do this?

I know how to call a command of ViewModel from Wizard but how I can tell "go ahead"?

Thanks.

4 Answers, 1 is accepted

Sort by
0
Martin Ivanov
Telerik team
answered on 20 Apr 2020, 09:51 AM

Hello Enrico,

You can achieve your requirement with a custom command provider. If you override the MoveCurrentToNext() method, you can implement a custom validation and if the data is invalid avoid returning the base implementation of the method in order to prevent the navigation. Here is an example in code:

public class CustomCommandProvider : WizardCommandProvider
{
	public CustomCommandProvider() : base(null)
	{
	}

	public CustomCommandProvider(RadWizard wizard) : base(wizard)
	{
	}

	protected override void MoveCurrentToNext()
	{
		if (the result from the database is valid)
		{
			base.MoveCurrentToNext();
		}
		else
		{
			// do nothing
		}
	}
}

<telerik:RadWizard>
	<telerik:RadWizard.CommandProvider>
		<local:CustomCommandProvider />
	</telerik:RadWizard.CommandProvider>
</telerik:RadWizard>

Can you please try this and let me know if it helps?

Regards,
Martin Ivanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
enrico
Top achievements
Rank 1
Veteran
answered on 22 Apr 2020, 01:23 PM

Hi Martin,
thanks for your reply.
I had tried to use CustomCommandProvider unsuccessful. The class is in the same namespace of ViewModel, but how I can access to the property of ViewModel?
I have this situation: first page of Wizard has a list of database Cconnection and the user have to select one. On next button I have to check that the selected connection is available.
This is my ViewModel, but in MoveCurrentToNext method how I can access to ViewModel property?

namespace Dinamo.ViewModels
{
    public class StartUpWizardWindowViewModel : BindableBase
    {
        public string Title => ProductInfos.ProductName;
 
        private ConnectionItem _connectionSelected;
        public ObservableCollection<ConnectionItem> ListDBConnections { get; set; }
        public ConnectionItem DBConnectionSelected
        {
            get => _connectionSelected;
            set
            {
                _connectionSelected = value;
                OnPropertyChanged(new PropertyChangedEventArgs("IsDBConnectionSelected"));
            }
        }
        public bool IsDBConnectionSelected
        {
            get
            {
                return _connectionSelected != null;
            }
        }
 
        public StartUpWizardWindowViewModel()
        {
            ListDBConnections = new ObservableCollection<ConnectionItem>(DBProfilesManager.Instance.GetConnectionList());
        }
   
    }
 
   public class CustomCommandProvider : WizardCommandProvider
        {
            public CustomCommandProvider() : base(null)
            {
            }
 
            public CustomCommandProvider(RadWizard wizard) : base(wizard)
            {
            }
 
            protected override void MoveCurrentToNext()
            {
                //How I can access to DBConnectionSelected?
                if (DBProfilesManager.Instance.TestConnection(DBConnectionSelected.ProfiloDB))
                {
                    base.MoveCurrentToNext();
                }
 
                else
                {
                    // do nothing
                }
            }
        }
 
}
0
Martin Ivanov
Telerik team
answered on 24 Apr 2020, 10:53 AM

Hello enrico,

On way to do this is to expose an additional property on the CustomCommandProvider class that holds a reference to the view model. For example:

public class CustomCommandProvider : WizardCommandProvider
{
	public ConnectionItem DBConnectionSelected
	{
		get { return (ConnectionItem )GetValue(DBConnectionSelectedProperty); }
		set { SetValue(DBConnectionSelectedProperty, value); }
	}

	public static readonly DependencyProperty DBConnectionSelectedProperty =
		DependencyProperty.Register(
			"DBConnectionSelected", 
			typeof(ConnectionItem), 
			typeof(CustomCommandProvider), 
			new PropertyMetadata(null));

	public CustomCommandProvider() : base(null)
	{
	}

	public CustomCommandProvider(RadWizard wizard) : base(wizard)
	{
	}

	protected override void MoveCurrentToNext()
	{
		if (DBProfilesManager.Instance.TestConnection(DBConnectionSelected.ProfiloDB))
		{
			base.MoveCurrentToNext();
		}

		else
		{
			// do nothing
		}
	}
}

Then bind it to the DBConnectionSelected property from the view model.

<telerik:RadWizard.CommandProvider>
	<local:CustomCommandProvider DBConnectionSelected="{Binding DBConnectionSelected}" />
</telerik:RadWizard.CommandProvider>

Regards,
Martin Ivanov
Progress Telerik

Progress is here for your business, like always. Read more about the measures we are taking to ensure business continuity and help fight the COVID-19 pandemic.
Our thoughts here at Progress are with those affected by the outbreak.
0
enrico
Top achievements
Rank 1
Veteran
answered on 27 Apr 2020, 10:35 AM

Thank you Martin.
I will try and I report to you the result.

 

Tags
Wizard
Asked by
enrico
Top achievements
Rank 1
Veteran
Answers by
Martin Ivanov
Telerik team
enrico
Top achievements
Rank 1
Veteran
Share this question
or