forked from chuongmep/CadPythonShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathViewModelBase.cs
More file actions
90 lines (81 loc) · 3.26 KB
/
ViewModelBase.cs
File metadata and controls
90 lines (81 loc) · 3.26 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
using System.ComponentModel;
using System.Diagnostics;
using System.Globalization;
using System.Reflection;
using System.Runtime.CompilerServices;
namespace CADSnoop.Model
{
public class ViewModelBase : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
// <summary>
/// Called when the specified property has been changed.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
/// <param name="oldValue">The old value.</param>
/// <param name="newValue">The new value.</param>
protected virtual void OnPropertyChanged(string propertyName, object oldValue, object newValue)
{
this.OnPropertyChanged(propertyName);
}
#region Can Optimize
/// <summary>
///
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="property"></param>
/// <param name="value"></param>
/// <param name="propertyName"></param>
public void OnPropertyChanged<T>(ref T property, T value, [CallerMemberName] string propertyName = "")
{
property = value;
var handler = PropertyChanged;
if (handler != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
#endregion
/// <summary>
/// Sets the property value.
/// </summary>
/// <typeparam name="T">The type of the property.</typeparam>
/// <param name="field">The field.</param>
/// <param name="value">The value.</param>
/// <param name="propertyName">Name of the property.</param>
/// <returns>
/// True if the property was set.
/// </returns>
/// <remarks>This method uses the CallerMemberNameAttribute to determine the property name.</remarks>
protected bool SetValue<T>(ref T field, T value, [System.Runtime.CompilerServices.CallerMemberName] string propertyName = "")
{
// ReSharper disable once RedundantNameQualifier
if (object.Equals(field, value))
{
return false;
}
this.VerifyProperty(propertyName);
//// this.OnPropertyChanging(propertyName, field, value);
T oldValue = field;
field = value;
this.OnPropertyChanged(propertyName, oldValue, value);
return true;
}
/// <summary>
/// Verifies the property name.
/// </summary>
/// <param name="propertyName">Name of the property.</param>
[Conditional("DEBUG")]
private void VerifyProperty(string propertyName)
{
var type = this.GetType();
// Look for a public property with the specified name.
var propertyInfo = type.GetTypeInfo().GetDeclaredProperty(propertyName);
Debug.Assert(propertyInfo != null, string.Format(CultureInfo.InvariantCulture, "{0} is not a property of {1}", propertyName, type.FullName));
}
}
}