TodoItemViewModel is the ViewModel class that represents each item in the to-do list on MainView. It does not have an entire view of its own (although it could have), but is instead rendered by a template in ListView. We will get back to this when we create the controls for MainView.
The important thing here is that this ViewModel object represents a single item, regardless of where we choose to render it.
Let's create the TodoItemViewModel class:
- Create a class called TodoItemViewModel in the ViewModels folder.
- Add the following template code and resolve the references:
public class TodoItemViewModel : ViewModel
{
public TodoItemViewModel(TodoItem item) => Item = item;
public event EventHandler ItemStatusChanged;
public TodoItem Item { get; private set; }
public string StatusText => Item.Completed ? "Reactivate" :
"Completed";
}
As with any other ViewModel class, we inherit the TodoItemViewModel class from ViewModel. We conform to the pattern of injecting all the dependencies into the constructor. In this case, we pass an instance of the TodoItem class to the constructor that the ViewModel object will use to expose to the view.
The ItemStatusChanged event handler will be used later when we want to signal to the view that the state of the TodoItem class has changed. The Item property allows us to access the item that we passed in.
The StatusText property is used to make the status of the to-do item human-readable in the view.