-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathBindable.cs
More file actions
65 lines (52 loc) · 1.98 KB
/
Bindable.cs
File metadata and controls
65 lines (52 loc) · 1.98 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq.Expressions;
using System.Runtime.CompilerServices;
namespace FFBitrateViewer
{
// Source: https://timoch.com/blog/2013/08/annoyed-with-inotifypropertychange/ (with some modifications)
/// <summary>
/// Base class to implement object that can be bound to
/// </summary>
public class Bindable : INotifyPropertyChanged
{
private readonly Dictionary<string, object?> _properties = new();
/// <summary>
/// Gets the value of a property
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="name"></param>
/// <returns></returns>
protected T? Get<T>([CallerMemberName] string? name = null)
{
Debug.Assert(name != null, "name != null");
if (_properties.TryGetValue(name, out object? value)) return value == null ? default : (T)value;
return default;
}
/// <summary>
/// Sets the value of a property
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="value"></param>
/// <param name="name"></param>
protected void Set<T>(T value, [CallerMemberName] string? name = null)
{
Debug.Assert(name != null, "name != null");
if (Equals(value, Get<T>(name))) return;
_properties[name] = value;
OnPropertyChanged(name);
}
public event PropertyChangedEventHandler? PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName] string? propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
protected virtual void OnPropertyChanged<T>(Expression<Func<T>> raiser)
{
var propertyName = ((MemberExpression)raiser.Body).Member.Name;
OnPropertyChanged(propertyName);
}
}
}