| | | 1 | | // ----------------------------------------------------------------------- |
| | | 2 | | // <copyright file="SelectionBehavior.cs" company="Stéphane ANDRE"> |
| | | 3 | | // Copyright (c) Stéphane ANDRE. All rights reserved. |
| | | 4 | | // </copyright> |
| | | 5 | | // ----------------------------------------------------------------------- |
| | | 6 | | |
| | | 7 | | namespace MyNet.Observable.Behaviors; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// Provides selection state (IsSelected / IsSelectable) for an ObservableObject. |
| | | 11 | | /// The behavior implements <see cref="ISelectable"/> and raises property changed |
| | | 12 | | /// notifications on the owner when the selection state changes. |
| | | 13 | | /// </summary> |
| | | 14 | | /// <remarks> |
| | | 15 | | /// Initializes a new instance of the <see cref="SelectionBehavior"/> class. |
| | | 16 | | /// </remarks> |
| | | 17 | | /// <param name="owner">The owner observable object.</param> |
| | | 18 | | /// <param name="isSelectable">Initial value for IsSelectable (default true).</param> |
| | 6 | 19 | | public sealed class SelectionBehavior(ObservableObject owner, bool isSelectable = true) : SuspendableBehavior<Observable |
| | | 20 | | { |
| | 6 | 21 | | private bool _isSelectable = isSelectable; |
| | | 22 | | private bool _isSelected; |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public bool IsSelectable |
| | | 26 | | { |
| | 0 | 27 | | get => _isSelectable; |
| | | 28 | | set |
| | | 29 | | { |
| | 3 | 30 | | if (IsDisposed || IsSuspended) |
| | 0 | 31 | | return; |
| | | 32 | | |
| | 3 | 33 | | if (_isSelectable == value) |
| | 0 | 34 | | return; |
| | | 35 | | |
| | 3 | 36 | | _isSelectable = value; |
| | | 37 | | |
| | 3 | 38 | | if (!_isSelectable) |
| | | 39 | | { |
| | 3 | 40 | | SetSelected(false); |
| | | 41 | | } |
| | | 42 | | |
| | 3 | 43 | | NotifyChanged(nameof(IsSelectable)); |
| | 3 | 44 | | } |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | /// <inheritdoc /> |
| | | 48 | | public bool IsSelected |
| | | 49 | | { |
| | 3 | 50 | | get => _isSelected; |
| | | 51 | | set |
| | | 52 | | { |
| | 6 | 53 | | if (IsDisposed || IsSuspended) |
| | 3 | 54 | | return; |
| | | 55 | | |
| | 3 | 56 | | if (!_isSelectable) |
| | 0 | 57 | | return; |
| | | 58 | | |
| | 3 | 59 | | SetSelected(value); |
| | 3 | 60 | | } |
| | | 61 | | } |
| | | 62 | | |
| | | 63 | | /// <summary> |
| | | 64 | | /// Sets the IsSelected property to the specified value and raises a property changed notification if the value has |
| | | 65 | | /// </summary> |
| | | 66 | | /// <param name="value">The new value for the IsSelected property.</param> |
| | | 67 | | private void SetSelected(bool value) |
| | | 68 | | { |
| | 6 | 69 | | if (_isSelected == value) |
| | 0 | 70 | | return; |
| | | 71 | | |
| | 6 | 72 | | _isSelected = value; |
| | | 73 | | |
| | 6 | 74 | | NotifyChanged(nameof(IsSelected)); |
| | 6 | 75 | | } |
| | | 76 | | |
| | | 77 | | /// <summary> |
| | | 78 | | /// Notifies the owner that a property has changed by raising the PropertyChanged event with the specified property |
| | | 79 | | /// </summary> |
| | | 80 | | /// <param name="propertyName">The name of the property that changed.</param> |
| | 9 | 81 | | private void NotifyChanged(string propertyName) => Owner.NotifyPropertyChanged(propertyName); |
| | | 82 | | } |
| | | 83 | | |