aboutsummaryrefslogtreecommitdiffstats
path: root/Software/Visual_Studio/Tango.SharedUI/DialogViewVM.cs
blob: 1854b410ab7ea62d0c0aebbf7f6943b73db78ea5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Tango.Core.Commands;
using Tango.SharedUI;

namespace Tango.SharedUI
{
    /// <summary>
    /// Represents a dialog view model base class.
    /// </summary>
    /// <seealso cref="Tango.SharedUI.ViewModel" />
    public abstract class DialogViewVM : ViewModel
    {
        public event Action Accepted;
        public event Action Canceled;

        /// <summary>
        /// Initializes a new instance of the <see cref="DialogViewVM"/> class.
        /// </summary>
        public DialogViewVM()
        {
            CanClose = true;
            CloseCommand = new RelayCommand(Cancel, (x) => CanClose);
            OKCommand = new RelayCommand(Accept, (x) => CanClose);
        }

        private bool _canClose;
        /// <summary>
        /// Gets or sets a value indicating whether this dialog can be closed.
        /// </summary>
        public bool CanClose
        {
            get { return _canClose; }
            set { _canClose = value; RaisePropertyChangedAuto(); InvalidateRelayCommands(); }
        }

        /// <summary>
        /// Gets a value indicating whether the dialog has been confirmed.
        /// </summary>
        public bool DialogResult { get; private set; }

        /// <summary>
        /// Gets or sets the close command.
        /// </summary>
        public RelayCommand CloseCommand { get; set; }

        /// <summary>
        /// Gets or sets the OK command.
        /// </summary>
        public RelayCommand OKCommand { get; set; }

        /// <summary>
        /// Called when the dialog has been shown.
        /// </summary>
        public virtual void OnShow()
        {

        }

        /// <summary>
        /// Invokes the <see cref="Accepted"/> event.
        /// </summary>
        protected virtual void Accept()
        {
            DialogResult = true;
            Accepted?.Invoke();
        }

        /// <summary>
        /// Invokes the <see cref="Canceled"/> event.
        /// </summary>
        protected virtual void Cancel()
        {
            Canceled?.Invoke();
        }
    }
}