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
|
using System;
using System.ComponentModel;
using System.Windows;
using System.Windows.Automation;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using Xamarin.PropertyEditing.Drawing;
using Xamarin.PropertyEditing.ViewModels;
namespace Xamarin.PropertyEditing.Windows
{
[TemplatePart (Name = "sizingButton", Type = typeof(ButtonBase))]
[TemplatePart (Name = "windowRect", Type = typeof (FrameworkElement))]
[TemplatePart (Name = "elementRect", Type = typeof (FrameworkElement))]
internal class AutoResizingMaskEditorControl
: PropertyEditorControl
{
public AutoResizingMaskEditorControl ()
{
DataContextChanged += OnDataContextChanged;
}
public override void OnApplyTemplate ()
{
base.OnApplyTemplate ();
this.sizingButton = GetTemplateChild ("sizingButton") as ButtonBase;
if (this.sizingButton == null)
throw new InvalidOperationException ($"Template for {nameof(AutoResizingMaskEditorControl)} must have a sizingButton");
this.window = GetTemplateChild ("windowRect") as FrameworkElement;
if (this.window == null)
throw new InvalidOperationException ($"Template for {nameof(AutoResizingMaskEditorControl)} must have a windowRect");
this.elementVisual = GetTemplateChild ("elementRect") as FrameworkElement;
if (this.elementVisual == null)
throw new InvalidOperationException ($"Template for {nameof(AutoResizingMaskEditorControl)} must have a elementRect");
this.window.SizeChanged += OnPreviewWindowSizeChanged;
UpdateSizeName();
UpdateVisual();
}
private AutoResizingPropertyViewModel vm;
private ButtonBase sizingButton;
private FrameworkElement elementVisual, window;
private void OnDataContextChanged (object sender, DependencyPropertyChangedEventArgs e)
{
if (this.vm != null) {
this.vm.PropertyChanged -= OnViewModelPropertyChanged;
}
this.vm = e.NewValue as AutoResizingPropertyViewModel;
if (this.vm != null) {
this.vm.PropertyChanged += OnViewModelPropertyChanged;
}
UpdateSizeName();
}
private void OnViewModelPropertyChanged (object sender, PropertyChangedEventArgs e)
{
if (e.PropertyName == nameof(AutoResizingPropertyViewModel.WidthSizable) || e.PropertyName == nameof(AutoResizingPropertyViewModel.HeightSizable)) {
UpdateSizeName ();
UpdateVisual();
} else if (e.PropertyName == nameof(AutoResizingPropertyViewModel.LeftMarginFixed)
|| e.PropertyName == nameof(AutoResizingPropertyViewModel.RightMarginFixed)
|| e.PropertyName == nameof(AutoResizingPropertyViewModel.TopMarginFixed)
|| e.PropertyName == nameof(AutoResizingPropertyViewModel.BottomMarginFixed)) {
UpdateVisual();
}
}
private void OnPreviewWindowSizeChanged (object sender, SizeChangedEventArgs e)
{
UpdateVisual ();
}
private void UpdateVisual ()
{
if (this.vm == null)
return;
var elementRect = this.vm.GetPreviewElementRectangle (new CommonSize (this.window.ActualWidth, this.window.ActualHeight), this.vm.Value);
Canvas.SetLeft (this.elementVisual, elementRect.X);
Canvas.SetTop (this.elementVisual, elementRect.Y);
this.elementVisual.Width = elementRect.Width;
this.elementVisual.Height = elementRect.Height;
}
private void UpdateSizeName ()
{
if (this.sizingButton == null)
return;
string name = null;
if (this.vm != null) {
string current;
if (!this.vm.HeightSizable && !this.vm.WidthSizable)
current = Properties.Resources.AutoresizingFixedSized;
else if (this.vm.HeightSizable && !this.vm.WidthSizable)
current = Properties.Resources.AutoresizingHeightSizable;
else if (!this.vm.HeightSizable && this.vm.WidthSizable)
current = Properties.Resources.AutoresizingWidthSizable;
else
current = Properties.Resources.AutoresizingWidthHeightSizable;
name = String.Format (Properties.Resources.AutoresizingSizingName, current);
}
AutomationProperties.SetName (this.sizingButton, name ?? String.Empty);
}
}
}
|