Skip to content

Commit 3d5109c

Browse files
committed
feactor: convert editing event handler to Command in ViewModel + change it from double click to simply an edit button
1 parent 4c82b00 commit 3d5109c

4 files changed

Lines changed: 11 additions & 16 deletions

File tree

Desktop/Project/TaskListViewModel.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,12 @@ public TaskListViewModel(
3131
Tasks.Add(taskCreationViewModel.CreatedTask);
3232
});
3333

34+
Edit = new RelayCommand<Task>(taskToEdit =>
35+
{
36+
var window = new TaskEditing(taskToEdit!);
37+
window.ShowDialog();
38+
});
39+
3440
Delete = new RelayCommand<Task>(taskToRemove =>
3541
{
3642
Tasks.Remove(taskToRemove!);
@@ -40,6 +46,7 @@ public TaskListViewModel(
4046

4147
public ICommand Add { get; }
4248

49+
public ICommand Edit { get; }
4350
public ICommand Delete { get; }
4451

4552
public void PopulateTasks()

Desktop/Project/TasksList.xaml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,6 @@
66
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
77
mc:Ignorable="d"
88
xmlns:domain="clr-namespace:Desktop.Domain">
9-
<UserControl.Resources>
10-
<Style TargetType="ListBoxItem">
11-
<EventSetter Event="MouseDoubleClick" Handler="Edit" />
12-
</Style>
13-
</UserControl.Resources>
149
<Grid>
1510
<StackPanel>
1611
<StackPanel Orientation="Horizontal">
@@ -23,6 +18,9 @@
2318
<DataTemplate DataType="{x:Type domain:Task}">
2419
<StackPanel Orientation="Horizontal">
2520
<TextBlock Text="{Binding Name}" />
21+
<Button Content="Edit"
22+
Command="{Binding DataContext.Edit, ElementName=Root}"
23+
CommandParameter="{Binding}" />
2624
<Button Content="Delete"
2725
Command="{Binding DataContext.Delete, ElementName=Root}"
2826
CommandParameter="{Binding}" />

Desktop/Project/TasksList.xaml.cs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System.Windows.Controls;
2-
using System.Windows.Input;
32
using Desktop.Tasks;
4-
using Task = Desktop.Domain.Task;
53

64
namespace Desktop.Project;
75

@@ -18,13 +16,4 @@ public TasksList(TaskListViewModel viewModel)
1816
viewModel.TaskCreationViewCreator = creationViewModel =>
1917
new TaskCreation(creationViewModel);
2018
}
21-
22-
private void Edit(object sender, MouseButtonEventArgs e)
23-
{
24-
if (sender is not ListBoxItem { DataContext: Task task })
25-
return;
26-
27-
var window = new TaskEditing(task);
28-
window.ShowDialog();
29-
}
3019
}

Desktop/Tasks/TaskEditing.xaml.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using System.Windows;
2+
using Desktop.Common;
23
using Task = Desktop.Domain.Task;
34

45
namespace Desktop.Tasks;

0 commit comments

Comments
 (0)