This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
Copy pathFormsButton.cs
110 lines (91 loc) · 2.71 KB
/
FormsButton.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
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
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
using WBrush = Windows.UI.Xaml.Media.Brush;
using WContentPresenter = Windows.UI.Xaml.Controls.ContentPresenter;
namespace Xamarin.Forms.Platform.UWP
{
public class FormsButton : Windows.UI.Xaml.Controls.Button
{
public static readonly DependencyProperty BorderRadiusProperty = DependencyProperty.Register(nameof(BorderRadius), typeof(int), typeof(FormsButton),
new PropertyMetadata(default(int), OnBorderRadiusChanged));
public static readonly DependencyProperty BackgroundColorProperty = DependencyProperty.Register(nameof(BackgroundColor), typeof(WBrush), typeof(FormsButton),
new PropertyMetadata(default(WBrush), OnBackgroundColorChanged));
WContentPresenter _contentPresenter;
Windows.UI.Xaml.Controls.Grid _rootGrid;
public WBrush BackgroundColor
{
get
{
return (WBrush)GetValue(BackgroundColorProperty);
}
set
{
SetValue(BackgroundColorProperty, value);
}
}
public int BorderRadius
{
get
{
return (int)GetValue(BorderRadiusProperty);
}
set
{
SetValue(BorderRadiusProperty, value);
}
}
protected override void OnApplyTemplate()
{
base.OnApplyTemplate();
_contentPresenter = GetTemplateChild("ContentPresenter") as WContentPresenter;
_rootGrid = GetTemplateChild("RootGrid") as Windows.UI.Xaml.Controls.Grid;
UpdateBackgroundColor();
UpdateBorderRadius();
}
static void OnBackgroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((FormsButton)d).UpdateBackgroundColor();
}
static void OnBorderRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
((FormsButton)d).UpdateBorderRadius();
}
void UpdateBackgroundColor()
{
if (BackgroundColor == null)
BackgroundColor = Background;
if (_contentPresenter != null)
_contentPresenter.Background = BackgroundColor;
Background = Color.Transparent.ToBrush();
}
void UpdateBorderRadius()
{
var radius = BorderRadius == -1 ? 0 : BorderRadius;
var cornerRadius = new Windows.UI.Xaml.CornerRadius(radius);
if (_contentPresenter != null)
_contentPresenter.CornerRadius = cornerRadius;
if(_rootGrid != null)
_rootGrid.CornerRadius = cornerRadius;
}
public void UpdateCharacterSpacing(int characterSpacing)
{
CharacterSpacing = characterSpacing;
if (_contentPresenter != null)
_contentPresenter.CharacterSpacing = CharacterSpacing;
if(Content is TextBlock tb)
{
tb.CharacterSpacing = CharacterSpacing;
}
if (Content is StackPanel sp)
{
foreach (var item in sp.Children)
{
if (item is TextBlock textBlock)
{
textBlock.CharacterSpacing = CharacterSpacing;
}
}
}
}
}
}