-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathUtils.ObservableProperties.cs
158 lines (145 loc) · 5.87 KB
/
Utils.ObservableProperties.cs
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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/**
* MIT License
*
* Copyright (c) 2017 "Nabil Redmann (BananaAcid) <[email protected]>"
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.ComponentModel;
namespace Utils
{
/// <summary>
/// have single properties to be observable (bindable) in wpf.
/// you have to bind the underlying value variable.
///
/// this is the opposite to the MSDN, stating, you should have a specific class, that is observable and should hold all required properties to be reflected by the WPF-XAML-UI.
/// </summary>
/// <see cref="http://stackoverflow.com/a/43835207/1644202"/>
public static class ObservableProperties
{
/// <summary>
/// a single observable type
/// </summary>
/// <typeparam name="T"></typeparam>
/// <example>
/// using static Utils.ObservableProperties;
///
/// Class MainWindow ...
/// public static Prop<bool> actionIsLoading { get; set; } = new Prop<bool>(); // without initial value
/// public static Prop<bool> actionIsLoading { get; set; } = new Prop<bool> { Value = false }; // with initial value
///
/// // initialize with user settings, need to be saved back on app exit
/// public static Prop<bool> optionStartMinimized { get; set; } = new Prop<bool> { Value = Properties.Settings.Default.optionStartMinimized };
/// // save it back to user settings:
/// // Properties.Settings.Default.optionStartMinimized = optionStartMinimized.Value;
/// // Properties.Settings.Default.save();
/// XAML
/// Value="{Binding actionIsLoading.Value}"
/// </example>
public class Prop<T> : ObservablePropertyBase
{
internal T _value;
public T Value
{
get { return _value; }
set { _value = value; NotifyPropertyChanged(); }
}
public static bool operator ==(Prop<T> self, T other)
{
return self._value.Equals(other);
}
public static bool operator !=(Prop<T> self, T other)
{
return !self._value.Equals(other);
}
public void NotifyPropertyChanged()
{
NotifyPropertyChanged("Value");
}
}
/// <summary>
/// a double type
/// </summary>
/// <typeparam name="T">internal Value number 1's type</typeparam>
/// <typeparam name="U">internal Value2 number 2's type </typeparam>
/// <example>
/// using static Utils.ObservableProperties;
///
/// Class MainWindow ...
/// public static PropProp<Visibility, int> isUpdateProgress { get; set; } = new PropProp<Visibility, int> { Value = Visibility.Hidden; Value2 = 0; };
/// XAML
/// ... Value="{Binding isUpdateProgress.Value}" ...
/// ... Value="{Binding isUpdateProgress.Value2}" ...
/// </example>
public class PropProp<T, U> : Prop<T>
{
internal U _value2;
public U Value2
{
get { return _value2; }
set { _value2 = value; NotifyPropertyChanged(); }
}
public new void NotifyPropertyChanged()
{
NotifyPropertyChanged("Value");
NotifyPropertyChanged("Value2");
}
}
/// <summary>
/// internal.
/// </summary>
public class ObservablePropertyBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
internal void NotifyPropertyChanged(String propertyName)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}
/// <summary>
/// extending observable collections to support ranges
/// </summary>
/// <typeparam name="T"></typeparam>
/// <example>
/// Class MainWindow ...
/// public static ObservableCollection<string> optionPathList { get; set; }
/// XAML
/// ... ItemsSource="{Binding optionPathList}" ...
/// </example>
public class ObservableCollectionEx<T> : ObservableCollection<T>
{
public void AddRange(IEnumerable<T> items)
{
foreach (var item in items)
{
this.Items.Add(item);
}
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Add));
}
public void NotifyCollectionChanged()
{
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
}