-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewModel.cs
More file actions
141 lines (128 loc) · 4.11 KB
/
ViewModel.cs
File metadata and controls
141 lines (128 loc) · 4.11 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
using System.ComponentModel;
using System.Windows;
/*
; Model-View-ViewModel (MVVM)
; https://learn.microsoft.com/en-us/dotnet/desktop/wpf/data/?view=netdesktop-8.0
; https://www.c-sharpcorner.com/article/datacontext-autowire-in-wpf/
; https://learn.microsoft.com/en-us/archive/msdn-magazine/2009/february/patterns-wpf-apps-with-the-model-view-viewmodel-design-pattern
; https://scottlilly.com/c-design-patterns-mvvm-model-view-viewmodel/
*/
namespace blekenbleu.jsonio
{
/// <summary>
/// define a class with Model-view-viewmodel pattern for dynamic UI
/// </summary>
public class ViewModel : INotifyPropertyChanged
{
readonly Control Ctrl; // Ctrl.Dispatcher.Invoke(Ctrl.Selected())
public ViewModel(Control C)
{
Ctrl = C;
}
// One event handler for all property changes
public event PropertyChangedEventHandler PropertyChanged;
// events to raise
readonly PropertyChangedEventArgs Bevent = new PropertyChangedEventArgs("ButtonVisibility");
readonly PropertyChangedEventArgs Cevent = new PropertyChangedEventArgs("ChangedVisibility");
readonly PropertyChangedEventArgs Nevent = new PropertyChangedEventArgs("SliderProperty");
readonly PropertyChangedEventArgs Sevent = new PropertyChangedEventArgs("SelectedProperty");
readonly PropertyChangedEventArgs SVevent = new PropertyChangedEventArgs("SliderVisibility");
readonly PropertyChangedEventArgs SLevent = new PropertyChangedEventArgs("SliderValue");
readonly PropertyChangedEventArgs Tevent = new PropertyChangedEventArgs("StatusText");
private Visibility _bvis = Visibility.Hidden; // until carID and game are defined
public Visibility ButtonVisibility // must be public for XAML Binding
{
get { return _bvis; }
set
{
if (_bvis != value)
{
_bvis = value;
PropertyChanged?.Invoke(this, Bevent);
}
}
}
private Visibility _cvis = Visibility.Hidden; // until carID and game are defined
public Visibility ChangedVisibility // must be public for XAML Binding
{
get { return _cvis; }
set
{
if (_cvis != value)
{
_cvis = value;
PropertyChanged?.Invoke(this, Cevent);
}
}
}
private double _sval = 40;
public double SliderValue // must be public for XAML Binding
{
get { return _sval; }
set
{
if (_sval != value)
{
_sval = value;
PropertyChanged?.Invoke(this, SLevent);
}
}
}
private Visibility _svis = Visibility.Hidden;
public Visibility SliderVisibility // must be public for XAML Binding
{
get { return _svis; }
set
{
if (_svis != value)
{
_svis = value;
PropertyChanged?.Invoke(this, SVevent);
}
}
}
private string _selected_Property = "unKnown";
public string SelectedProperty // must be public for XAML Binding
{
get { return _selected_Property; }
set
{
if (value != _selected_Property)
{
_selected_Property = value;
PropertyChanged?.Invoke(this, Sevent);
// update xaml DataGrid from another thread
Ctrl.Dispatcher.Invoke(() => Ctrl.Selected());
}
}
}
private string _slider_Property = "";
public string SliderProperty // must be public for XAML Binding
{
get { return _slider_Property; }
set
{
if (value != _slider_Property)
{
_slider_Property = value;
PropertyChanged?.Invoke(this, Nevent);
SliderVisibility = Visibility.Visible;
}
}
}
static internal readonly string statusText = "To enable: launch game or Replay";
private string _statusText = statusText;
public string StatusText // must be public for XAML Binding
{
get { return _statusText; }
set
{
if (value != _statusText)
{
_statusText = value;
PropertyChanged?.Invoke(this, Tevent);
}
}
}
} // public class StaticControl
}