Preparing AddEditViewModel
In this step you will prepare the view model responsible for adding a new car to the database or editing an existing one. You will define the class, the messages used in it, the required fields and properties.
- In the ViewModels folder add a new class named AddEditViewModel.
-
Inherit ViewModelBase with type parameter IDialogView.
public class AddEditViewModel : ViewModelBase<IDialogView> { }
Public Class AddEditViewModel Inherits ViewModelBase(Of IDialogView) End Class
-
Define the messages which will be needed throughout the view model.
private const string EDIT_MODE_TITLE = "Edit"; private const string ADD_MODE_TITLE = "Add"; private const string SAVE_ERROR_MESSAGE = "Saving operation failed! Internal error has occured. Exception message:\r\n"; private const string SAVE_ERROR_TITLE = "Error";
Private Const EDIT_MODE_TITLE As String = "Edit" Private Const ADD_MODE_TITLE As String = "Add" Private Const SAVE_ERROR_MESSAGE As String = "Saving operation failed! Internal error has occured. Exception message:" + vbCrLf Private Const SAVE_ERROR_TITLE As String = "Error"
-
Define the fields which will be used through out the view model. context is the OpenAccessContext object through which new cars will be added to the database and existing ones will be updated. title will contain the title of the associated view. currentCarId will contain the id of the car which is to be edited if the view model is initialized with such. currentCar will contain the car object with which the view model will work. It will either be an already existing car, retrieved via its Id, or it will be a newly initialized car which is to be inserted in the database. command is an object containing a command which can be bound to controls on the view.
private readonly SofiaCarRentalContext context; private string title; private int? currentCarId; private Car currentCar; private RelayCommand command;
Private ReadOnly _context As SofiaCarRentalContext Private _title As String Private _currentCarId As Integer? Private _currentCar As Car Private _command As RelayCommand
-
Define properties for title and currentCar.
public string Title { get { return this.title; } private set { this.title = value.Trim(); } } public Car CurrentCar { get { return this.currentCar; } private set { this.currentCar = value; } }
Public Property Title As String Get Return Me._title End Get Private Set(value As String) Me._title = value.Trim() End Set End Property Public Property CurrentCar As Car Get Return Me._currentCar End Get Private Set(value As Car) Me._currentCar = value End Set End Property
-
Add a property of type bool? named OperationIsSuccessful. This property will give access the DialogResult property of the associated view.
public bool? OperationIsSuccessful { get { return this.View.DialogResult; } private set { this.View.DialogResult = value; } }
Public Property OperationIsSuccessful As Boolean? Get Return MyBase.View.DialogResult End Get Private Set(value As Boolean?) MyBase.View.DialogResult = value End Set End Property
-
Define the constructor of the view model. In it you will need to initialize the context and set currentCarId if available. The base constructor is also called with a new instance of the associated view.
public AddEditViewModel(int? currentCarId) : base(new AddEditWindow()) { this.context = new SofiaCarRentalContext(); this.currentCarId = currentCarId; }
Public Sub New(currentCarId As Integer?) MyBase.New(New AddEditWindow()) Me._context = New SofiaCarRentalContext() Me._title = String.Empty Me._currentCarId = currentCarId End Sub
-
Create a new method named ShowDialog. In it you need to call the ShowDialog method of the related view and returns the result from it.
public bool? ShowDialog() { return this.View.ShowDialog(); }
Public Function ShowDialog() As Boolean? Return MyBase.View.ShowDialog() End Function