An easy MVVM framework to use in your WPF applications.
Inherit your ViewModels that are not Dialogs from BaseViewModel
to notify binded property changes.
public class MainViewModel : BaseViewModel
{
private string _test;
public string Test
{
get => _test;
set
{
_test = value;
OnPropertyChanged();
}
}
}
The DelegateCommand
class provide a mechanism to bind Commands and Events from view to viewmodel.
public class MainViewModel : BaseViewModel
{
public DelegateCommand TestCommand => new DelegateCommand(Test);
private void Test(object parameter)
{
//do something...
}
}
You can also use a method Can to determine when a Command can be executed.
public class MainViewModel : BaseViewModel
{
public DelegateCommand TestCommand => new DelegateCommand(Test, CanTest);
private void Test(object parameter)
{
//do something...
}
private bool CanTest()
{
return true;
}
}
The DialogManager
static class allows opening Windows (dialogs) that the corresponding ViewModel inherits from DialogViewModel
.
public class SampleViewModel : DialogViewModel
{
}
public class MainViewModel : BaseViewModel
{
private void ShowExempleDialog()
{
bool? dialogResult = DialogManager.ShowDialog(new SampleViewModel());
}
}
In this case, the ShowDialog
method will look for the name of a View that matches the name of the ViewModel by replacing the word "ViewModel" with "View".
ViewModel SampleViewModel
matches SampleView
dialog.