| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="ListDialogViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System; |
| | | 8 | | using System.Collections.Generic; |
| | | 9 | | using System.Windows.Input; |
| | | 10 | | using MyNet.Globalization.Culture; |
| | | 11 | | using MyNet.UI.Commands; |
| | | 12 | | using MyNet.UI.ViewModels.List; |
| | | 13 | | |
| | | 14 | | namespace MyNet.UI.ViewModels.Dialog; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Provides a base implementation for dialog view models that display a list of items and allow the user to select one |
| | | 18 | | /// </summary> |
| | | 19 | | /// <typeparam name="T">The type of the items in the list.</typeparam> |
| | | 20 | | /// <typeparam name="TList">The type of the list view model.</typeparam> |
| | | 21 | | public class ListDialogViewModel<T, TList> : DialogViewModel<IReadOnlyCollection<T>> |
| | | 22 | | where T : notnull |
| | | 23 | | where TList : IListViewModel<T> |
| | | 24 | | { |
| | | 25 | | /// <summary> |
| | | 26 | | /// Gets the list view model containing the items to be displayed in the dialog. This property is initialized throug |
| | | 27 | | /// </summary> |
| | | 28 | | public TList List { get; } |
| | | 29 | | |
| | | 30 | | /// <summary> |
| | | 31 | | /// Gets the command to execute when the user validates their selection and closes the dialog. This command is initi |
| | | 32 | | /// </summary> |
| | | 33 | | public ICommand ValidateCommand { get; } |
| | | 34 | | |
| | | 35 | | /// <summary> |
| | | 36 | | /// Gets the command to execute when the user cancels the dialog. This command is initialized through the constructo |
| | | 37 | | /// </summary> |
| | | 38 | | public ICommand CancelCommand { get; } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Initializes a new instance of the <see cref="ListDialogViewModel{T, TList}"/> class with the specified list view |
| | | 42 | | /// </summary> |
| | | 43 | | /// <param name="list">The list view model containing the items to be displayed in the dialog.</param> |
| | | 44 | | /// <param name="commandFactory">An optional command factory to create commands.</param> |
| | | 45 | | /// <param name="cultureService">Optional culture service used to manage culture changes.</param> |
| | | 46 | | /// <exception cref="ArgumentNullException">Thrown if the list parameter is null.</exception> |
| | | 47 | | public ListDialogViewModel( |
| | | 48 | | TList list, |
| | | 49 | | ICommandFactory? commandFactory = null, |
| | | 50 | | ICultureService? cultureService = null) |
| | 0 | 51 | | : base(commandFactory, cultureService) |
| | | 52 | | { |
| | 0 | 53 | | ArgumentNullException.ThrowIfNull(list); |
| | 0 | 54 | | List = list; |
| | | 55 | | |
| | 0 | 56 | | ValidateCommand = Commands.Create(Validate, CanValidate); |
| | 0 | 57 | | CancelCommand = Commands.Create(Cancel); |
| | 0 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <summary> |
| | | 61 | | /// Validates the user's selection and closes the dialog, returning the selected items from the list as a read-only |
| | | 62 | | /// </summary> |
| | 0 | 63 | | protected virtual void Validate() => Close([.. List.Items]); |
| | | 64 | | |
| | | 65 | | /// <summary> |
| | | 66 | | /// Cancels the dialog without returning any selected items. This method is called when the Cancel command is execut |
| | | 67 | | /// </summary> |
| | | 68 | | protected virtual void Cancel() |
| | | 69 | | { |
| | 0 | 70 | | SetCancelled(); |
| | 0 | 71 | | RequestClose(); |
| | 0 | 72 | | } |
| | | 73 | | |
| | | 74 | | /// <summary> |
| | | 75 | | /// Determines whether the Validate command can execute. This method returns true if the Validate command can be exe |
| | | 76 | | /// </summary> |
| | | 77 | | /// <returns>True if the Validate command can execute; otherwise, false.</returns> |
| | 0 | 78 | | protected virtual bool CanValidate() => true; |
| | | 79 | | |
| | | 80 | | /// <summary> |
| | | 81 | | /// Raises <see cref="ICommand.CanExecuteChanged"/> for <see cref="ValidateCommand"/>. |
| | | 82 | | /// </summary> |
| | 0 | 83 | | protected void RaiseValidateCanExecuteChanged() => (ValidateCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChan |
| | | 84 | | } |
| | | 85 | | |