-
Notifications
You must be signed in to change notification settings - Fork 1
/
DefaultModel.cs
46 lines (41 loc) · 1.86 KB
/
DefaultModel.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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq.Expressions;
using System.Text;
namespace SSHMan
{
/// <summary>
/// Implements a base class for all viewmodel classes
/// that implements <seealso cref="INotifyPropertyChanged"/> interface for binding.
/// </summary>
public class DefaultModel : INotifyPropertyChanged
{
/// <summary>
/// Standard implementation of <seealso cref="INotifyPropertyChanged"/>.
/// </summary>
public event PropertyChangedEventHandler PropertyChanged;
/// <summary>
/// Tell bound controls (via WPF binding) to refresh their display.
///
/// Sample call: this.NotifyPropertyChanged(() => this.IsSelected);
/// where 'this' is derived from <seealso cref="DefaultModel"/>
/// and IsSelected is a property.
/// </summary>
/// <typeparam name="TProperty"></typeparam>
/// <param name="property"></param>
public void RaisePropertyChanged<TProperty>(Expression<Func<TProperty>> property)
{
if(property == null) {throw new ArgumentNullException(nameof(property));}
var lambda = (LambdaExpression)property;
var memberExpression = lambda.Body is UnaryExpression unaryExpression ? (MemberExpression)unaryExpression.Operand : (MemberExpression)lambda.Body;
this.RaisePropertyChanged(memberExpression.Member.Name);
}
/// <summary>
/// Tell bound controls (via WPF binding) to refresh their display.
/// Standard implementation through <seealso cref="INotifyPropertyChanged"/>.
/// </summary>
/// <param name="propertyName"></param>
protected virtual void RaisePropertyChanged(string propertyName) => this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}