I am working on a task list app on WPF that I am new to, and being using MVVM pattern. What I want is to do is to make the X button on the left of the task to delete it after being pressed. According to MVVM pattern, I've created a MainViewModel Class where the TaskList initializes (I don't know if it is a right way, but if you know better, please tell me):
namespace TaskListApp.MVVM.Viewmodel
{
class MainViewModel
{
public ObservableCollection<TaskModel> Tasks { get; set; }
public ObservableCollection<TaskListModel> TaskList { get; set; }
public MainViewModel()
{
Tasks = new ObservableCollection<TaskModel>();
TaskList = new ObservableCollection<TaskListModel>();
TaskList.Add(new TaskListModel
{
TaskListItemName = "Today"
});
TaskList.Add(new TaskListModel
{
TaskListItemName = "This Week"
});
TaskList.Add(new TaskListModel
{
TaskListItemName = "This Month"
});
Tasks.Add(new TaskModel
{
TaskText = "Floss",
TaskIndex = 0,
IsDone = true
});
Tasks.Add(new TaskModel
{
TaskText = "Do dishes",
TaskIndex = 1,
IsDone = false
});
Tasks.Add(new TaskModel
{
TaskText = "Exercise",
TaskIndex = 2,
IsDone = false
});
Tasks.Add(new TaskModel
{
TaskText = "Drink water",
TaskIndex = 3,
IsDone = true
});
}
}
}
Then I connected it via DataContext:
<Window x:Class="TaskListApp.MainWindow" ...>
<Window.DataContext>
<viewmodel:MainViewModel/>
</Window.DataContext>
...</Window>
ListView in MainWindow looks like this:
<ListView ItemsSource="{Binding Tasks}"
ItemContainerStyle="{StaticResource TasksTheme}"
...some other properties...>
</ListView>
ListViewItem has a custom theme. To simplify, the template is:
<CheckBox IsChecked="{Binding IsDone}"
Style="{StaticResource CheckboxTheme}"/>
<TextBox Text="{Binding TaskText}"/>
<Button Click="TaskDelete_ButtonClick"/>
So I want to make button delete the ListViewItem it belongs to, but I don't see any way to do it with my knowledge. I think that I have to create a removing method inside MainViewModel, but I can't access it because I can't create another instance of it (I guess the one and only instance of that class initializes when app runs). Making this class static is no option too. So, please, share some knowledge about this kind of situations.

