This repository was archived by the owner on Jun 21, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Expand file tree
/
Copy pathViewBase.cs
More file actions
68 lines (62 loc) · 2.31 KB
/
Copy pathViewBase.cs
File metadata and controls
68 lines (62 loc) · 2.31 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
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Automation.Peers;
using GitHub.ViewModels;
using ReactiveUI;
using System.Reactive.Linq;
namespace GitHub.UI
{
/// <summary>
/// Base class for views.
/// </summary>
public class ViewBase<TInterface, TImplementor> : UserControl, IViewFor<TInterface>
where TInterface : class, IViewModel
where TImplementor : class
{
public static readonly DependencyProperty ViewModelProperty = DependencyProperty.Register(
"ViewModel", typeof(TInterface), typeof(TImplementor), new PropertyMetadata(null));
/// <summary>
/// Initializes a new instance of the <see cref="ViewBase{TInterface, TImplementor}"/> class.
/// </summary>
public ViewBase()
{
DataContextChanged += (s, e) => ViewModel = (TInterface)e.NewValue;
this.WhenAnyValue(x => x.ViewModel).Skip(1).Subscribe(x => DataContext = x);
}
/// <summary>
/// Gets or sets the control's data context as a typed view model.
/// </summary>
public TInterface ViewModel
{
get { return (TInterface)GetValue(ViewModelProperty); }
set { SetValue(ViewModelProperty, value); }
}
/// <summary>
/// Gets or sets the control's data context as a typed view model. Required for interaction
/// with ReactiveUI.
/// </summary>
TInterface IViewFor<TInterface>.ViewModel
{
get { return ViewModel; }
set { ViewModel = value; }
}
/// <summary>
/// Gets or sets the control's data context. Required for interaction with ReactiveUI.
/// </summary>
object IViewFor.ViewModel
{
get { return ViewModel; }
set { ViewModel = (TInterface)value; }
}
/// <summary>
/// Add an automation peer to views and custom controls
/// They do not have automation peers or properties by default
/// https://stackoverflow.com/questions/30198109/automationproperties-automationid-on-custom-control-not-exposed
/// </summary>
protected override AutomationPeer OnCreateAutomationPeer()
{
return new UIElementAutomationPeer(this);
}
}
}