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
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
Our thoughts here at Progress are with those affected by the outbreak.
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
}
}
}
}
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
Our thoughts here at Progress are with those affected by the outbreak.
Thank you Martin.
I will try and I report to you the result.