| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SelectionDialogViewModel.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | using System.ComponentModel; |
| | | 8 | | using System.Windows.Input; |
| | | 9 | | using MyNet.Observable; |
| | | 10 | | using MyNet.Observable.Collections.Selection; |
| | | 11 | | using MyNet.UI.Commands; |
| | | 12 | | using MyNet.UI.ViewModels.List.Selection; |
| | | 13 | | |
| | | 14 | | namespace MyNet.UI.ViewModels.Dialog; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// ViewModel for a dialog allowing to select one or more items from a list. The selected items are returned when the di |
| | | 18 | | /// </summary> |
| | | 19 | | /// <typeparam name="T">The type of the items in the list.</typeparam> |
| | | 20 | | public class SelectionDialogViewModel<T> : ListDialogViewModel<T, ISelectableListViewModel<T>> |
| | | 21 | | where T : notnull |
| | | 22 | | { |
| | | 23 | | /// <summary> |
| | | 24 | | /// Gets the command to execute when an item is double-clicked in the list. This command will validate the selection |
| | | 25 | | /// </summary> |
| | | 26 | | public ICommand DoubleClickCommand { get; } |
| | | 27 | | |
| | | 28 | | /// <summary> |
| | | 29 | | /// Initializes a new instance of the <see cref="SelectionDialogViewModel{T}"/> class with the specified selectable |
| | | 30 | | /// </summary> |
| | | 31 | | /// <param name="list">The selectable list to be displayed in the dialog.</param> |
| | | 32 | | /// <param name="commandFactory">An optional command factory to create commands.</param> |
| | | 33 | | public SelectionDialogViewModel(ISelectableListViewModel<T> list, ICommandFactory? commandFactory = null) |
| | 0 | 34 | | : base(list, commandFactory) |
| | | 35 | | { |
| | | 36 | | DoubleClickCommand = Commands.Create(Validate, () => List is { SelectedCount: > 0, SelectionMode: SelectionMode. |
| | | 37 | | |
| | 0 | 38 | | if (List is ObservableObject observableList) |
| | 0 | 39 | | observableList.PropertyChanged += HandleListPropertyChanged; |
| | 0 | 40 | | } |
| | | 41 | | |
| | | 42 | | /// <summary> |
| | | 43 | | /// Validates the selection and closes the dialog, returning the selected items. This method is called when the Vali |
| | | 44 | | /// </summary> |
| | 0 | 45 | | protected override void Validate() => Close([.. List.SelectedItems]); |
| | | 46 | | |
| | | 47 | | /// <summary> |
| | | 48 | | /// Determines whether the Validate command can execute. This method returns true if there is at least one selected |
| | | 49 | | /// </summary> |
| | | 50 | | /// <returns>True if the Validate command can execute; otherwise, false.</returns> |
| | 0 | 51 | | protected override bool CanValidate() => List.SelectedCount > 0; |
| | | 52 | | |
| | | 53 | | private void HandleListPropertyChanged(object? sender, PropertyChangedEventArgs e) |
| | | 54 | | { |
| | 0 | 55 | | if (e.PropertyName != nameof(ISelectableListViewModel<>.SelectedCount) && e.PropertyName != nameof(ISelectableLi |
| | 0 | 56 | | return; |
| | | 57 | | |
| | 0 | 58 | | (DoubleClickCommand as IRaiseCanExecuteChanged)?.RaiseCanExecuteChanged(); |
| | 0 | 59 | | RaiseValidateCanExecuteChanged(); |
| | 0 | 60 | | } |
| | | 61 | | |
| | | 62 | | /// <inheritdoc /> |
| | | 63 | | protected override void DisposeManagedResources() |
| | | 64 | | { |
| | 0 | 65 | | if (List is ObservableObject observableList) |
| | 0 | 66 | | observableList.PropertyChanged -= HandleListPropertyChanged; |
| | | 67 | | |
| | 0 | 68 | | base.DisposeManagedResources(); |
| | 0 | 69 | | } |
| | | 70 | | } |
| | | 71 | | |