Skip to content

Commit

Permalink
WatchList.WPF - Add "CustomDataGrid" to select multiple lines and del…
Browse files Browse the repository at this point in the history
…ete them.
  • Loading branch information
Stan-Kudri committed Dec 9, 2024
1 parent f1b2a73 commit d3c126a
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 64 deletions.
24 changes: 24 additions & 0 deletions WatchList.WPF/Component/CustomDataGrid.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections;
using System.Windows;
using System.Windows.Controls;

namespace WatchList.WPF.Component
{
public class CustomDataGrid : DataGrid
{
public CustomDataGrid()
=> SelectionChanged += CustomDataGrid_SelectionChanged;

void CustomDataGrid_SelectionChanged(object sender, SelectionChangedEventArgs e)
=> SelectedItemsList = SelectedItems;

public IList SelectedItemsList
{
get => (IList)GetValue(SelectedItemsListProperty);
set => SetValue(SelectedItemsListProperty, value);
}

public static readonly DependencyProperty SelectedItemsListProperty
= DependencyProperty.Register("SelectedItemsList", typeof(IList), typeof(CustomDataGrid), new PropertyMetadata(null));
}
}
16 changes: 0 additions & 16 deletions WatchList.WPF/Models/ModelDataLoad/DialogReplaceQuestionManager.cs

This file was deleted.

41 changes: 39 additions & 2 deletions WatchList.WPF/ViewModel/CinemaPageViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using System.Collections;
using System.Collections.ObjectModel;
using System.Windows;
using DevExpress.Mvvm;
Expand All @@ -20,6 +21,8 @@ namespace WatchList.WPF.ViewModel
{
public class CinemaPageViewModel : BindableBase
{
private const string HighlightTheDesiredLine = "No items selected.";

private readonly WatchItemService _itemService;
private readonly IMessageBox _messageBox;
private readonly ILogger<WatchItemRepository> _logger;
Expand All @@ -29,11 +32,13 @@ public class CinemaPageViewModel : BindableBase
private readonly FilterItemModel _filterItem;
private readonly ItemSearchRequest _searchRequests;

private readonly bool _isAscending = true;

private ObservableCollection<WatchItem> _watchItems = new ObservableCollection<WatchItem>();

private PagedList<WatchItem> _pagedList;
private readonly bool _isAscending = true;

private IList _selectItems = new ArrayList();
private int _curPage;

private int CurPage
Expand Down Expand Up @@ -61,6 +66,12 @@ public CinemaPageViewModel(IMessageBox messageBox,
LoadDataAsync();
}

public IList SelectItems
{
get => _selectItems;
set => SetValue(ref _selectItems, value);
}

public ObservableCollection<WatchItem> WatchItems
{
get => _watchItems;
Expand All @@ -85,7 +96,8 @@ public RelayCommandApp MoveToLastPage

public RelayCommandApp AddItemCommand
=> new RelayCommandApp(async async => await MoveAddItem());

public RelayCommandApp DeleteItemsCommand
=> new RelayCommandApp(async async => await DeleteItems());
public RelayCommandApp AddDataDBCommand
=> new RelayCommandApp(async async => await MoveAddData());

Expand All @@ -100,6 +112,31 @@ private async Task MoveAddItem()
await LoadDataAsync();
}

private async Task DeleteItems()
{
if (SelectItems.Count == 0)
{
await _messageBox.ShowInfo(HighlightTheDesiredLine);
return;
}

if (!await _messageBox.ShowQuestion("Delete select items?"))
{
return;
}

foreach (var item in SelectItems)
{
var isWatchItem = item as WatchItem;
if (isWatchItem != null)
{
_itemService.Remove(isWatchItem.Id);
}
}

await LoadDataAsync();
}

private async Task MoveAddData()
{
var addDataWindow = new MergeDatabaseWindow();
Expand Down
18 changes: 7 additions & 11 deletions WatchList.WPF/ViewModel/DataReplaceMessageVM.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
using System.Windows;
using CommunityToolkit.Mvvm.Input;
using WatchList.Core.Model.QuestionResult;
using WatchList.WPF.Models.ModelDataLoad;

namespace WatchList.WPF.ViewModel
{
Expand All @@ -11,16 +10,13 @@ public class DataReplaceMessageVM

private readonly string _titleLabel;

public DataReplaceMessageVM(string titleItem, DialogReplaceQuestionManager questionResult)
{
_titleLabel = titleItem;
ResultQuestion = questionResult;
}
public DataReplaceMessageVM(string titleItem)
=> _titleLabel = titleItem;

public string QuestionLabel => Question;
public string TitleLabel => _titleLabel;

public DialogReplaceQuestionManager ResultQuestion { get; private set; }
public DialogReplaceItemQuestion ResultQuestion { get; private set; } = DialogReplaceItemQuestion.Unknown;

public RelayCommand<Window> MoveYesCommand => new(MoveYesClick);
public RelayCommand<Window> MoveAllYesCommand => new(MoveAllYesClick);
Expand All @@ -29,28 +25,28 @@ public DataReplaceMessageVM(string titleItem, DialogReplaceQuestionManager quest

private void MoveYesClick(Window window)
{
ResultQuestion.UpdateData(DialogReplaceItemQuestion.Yes);
ResultQuestion = DialogReplaceItemQuestion.Yes;
window.DialogResult = true;
window?.Close();
}

private void MoveAllYesClick(Window window)
{
ResultQuestion.UpdateData(DialogReplaceItemQuestion.AllYes);
ResultQuestion = DialogReplaceItemQuestion.AllYes;
window.DialogResult = true;
window?.Close();
}

private void MoveNoClick(Window window)
{
ResultQuestion.UpdateData(DialogReplaceItemQuestion.No);
ResultQuestion = DialogReplaceItemQuestion.No;
window.DialogResult = true;
window?.Close();
}

private void MoveAllNoClick(Window window)
{
ResultQuestion.UpdateData(DialogReplaceItemQuestion.AllNo);
ResultQuestion = DialogReplaceItemQuestion.AllNo;
window.DialogResult = true;
window?.Close();
}
Expand Down
34 changes: 19 additions & 15 deletions WatchList.WPF/Views/CinemaPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
xmlns:local="clr-namespace:WatchList.WPF.Views"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
xmlns:local="clr-namespace:WatchList.WPF.Component"
Title="CinemaPage"
DataContext="{Binding CinemaPageViewModel, Source={StaticResource ViewModelLocator}}"
MinHeight="300" MinWidth="300">
Expand All @@ -26,24 +25,28 @@
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<ui:DataGrid Grid.Row="1"
ColumnWidth="Auto"
Grid.ColumnSpan="8"
FontSize="12"
AutoGenerateColumns="False"
FontStretch="ExtraCondensed"
IsReadOnly="True"
ItemsSource="{Binding WatchItems, Mode=OneWay}">
<DataGrid.Columns>
<local:CustomDataGrid Grid.Row="1"
Grid.ColumnSpan="8"
ColumnWidth="Auto"
FontSize="12"
AutoGenerateColumns="False"
FontStretch="ExtraCondensed"
IsReadOnly="True"
SelectionMode="Extended"
SelectionUnit="FullRow"
SnapsToDevicePixels="True"
SelectedItemsList="{Binding SelectItems, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
ItemsSource="{Binding WatchItems}">
<local:CustomDataGrid.Columns>
<DataGridTextColumn Header="Title" Binding="{Binding Path=Title}" Width="Auto" />
<DataGridTextColumn Header="Season/Part" Binding="{Binding Path=Sequel}" Width="Auto" />
<DataGridTextColumn Header="Status" Binding="{Binding Path=Status}" Width="Auto" />
<DataGridTextColumn Header="Date" Binding="{Binding Path=Date, StringFormat=dd MMMM yyyy}" Width="Auto"/>
<DataGridTextColumn Header="Grade" Binding="{Binding Path=Grade}" Width="Auto" />
<DataGridTextColumn Header="Type" Binding="{Binding Path=Type}" Width="Auto" />
<DataGridTextColumn Header="Id" Binding="{Binding Path=Id}" Width="Auto" />
</DataGrid.Columns>
</ui:DataGrid>
<DataGridTextColumn Header="Id" Binding="{Binding Path=Id}" Width="Auto"/>
</local:CustomDataGrid.Columns>
</local:CustomDataGrid>
<Grid Grid.Column="1" Grid.Row="2" Grid.ColumnSpan="6">
<StackPanel Grid.Row="1" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Margin="2 2 2 2"
Expand Down Expand Up @@ -127,7 +130,8 @@
FontSize="12" FontWeight="DemiBold"
AutomationProperties.Name="Btn_Delete_Cinema"
Background="AliceBlue"
Focusable="False" />
Focusable="False"
Command="{Binding DeleteItemsCommand}"/>
<Button
Content="Add Data"
HorizontalAlignment="Stretch"
Expand Down
13 changes: 0 additions & 13 deletions WatchList.WPF/Views/CinemaPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,17 +1,4 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WatchList.WPF.Views
{
Expand Down
5 changes: 2 additions & 3 deletions WatchList.WPF/Views/Message/DataReplaceMessageWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
using WatchList.WPF.Models.ModelDataLoad;
using WatchList.WPF.ViewModel;

namespace WatchList.WPF.Views.Message
Expand All @@ -8,10 +7,10 @@ namespace WatchList.WPF.Views.Message
/// </summary>
public partial class DataReplaceMessageWindow
{
public DataReplaceMessageWindow(string titleName, DialogReplaceQuestionManager replaceQuestionManager)
public DataReplaceMessageWindow(DataReplaceMessageVM viewModel)
{
InitializeComponent();
DataContext = new DataReplaceMessageVM(titleName, replaceQuestionManager);
DataContext = viewModel;
}
}
}
8 changes: 4 additions & 4 deletions WatchList.WPF/Views/Message/MessageWindow.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using System.Windows;
using WatchList.Core.Model.QuestionResult;
using WatchList.Core.Service.Component;
using WatchList.WPF.Models.ModelDataLoad;
using WatchList.WPF.ViewModel;
using MessageBoxWindow = System.Windows.MessageBox;

namespace WatchList.WPF.Views.Message
Expand All @@ -10,11 +10,11 @@ public class MessageWindow : IMessageBox
{
public Task<DialogReplaceItemQuestion> ShowDataReplaceQuestion(string titleItem)
{
var replaceQuestionManager = new DialogReplaceQuestionManager();
var windowDataReplaceItem = new DataReplaceMessageWindow(titleItem, replaceQuestionManager);
var dataReplaceMessageVM = new DataReplaceMessageVM(titleItem);
var windowDataReplaceItem = new DataReplaceMessageWindow(dataReplaceMessageVM);

return windowDataReplaceItem.ShowDialog() == true
? Task.FromResult(replaceQuestionManager.DialogReplaceItemQuestion)
? Task.FromResult(dataReplaceMessageVM.ResultQuestion)
: Task.FromResult(DialogReplaceItemQuestion.Unknown);
}

Expand Down

0 comments on commit d3c126a

Please sign in to comment.