-
Notifications
You must be signed in to change notification settings - Fork 1.4k
/
Copy pathColorPickerButton.cs
157 lines (135 loc) · 5.78 KB
/
ColorPickerButton.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml;
using Windows.UI.Xaml.Controls;
namespace Microsoft.Toolkit.Uwp.UI.Controls
{
/// <summary>
/// A <see cref="DropDownButton"/> which displays a color as its <c>Content</c> and it's <c>Flyout</c> is a <see cref="ColorPicker"/>.
/// </summary>
[TemplatePart(Name = nameof(CheckeredBackgroundBorder), Type = typeof(Border))]
public partial class ColorPickerButton : DropDownButton
{
/// <summary>
/// Gets the <see cref="Controls.ColorPicker"/> instances contained by the <see cref="DropDownButton"/>.
/// </summary>
public ColorPicker ColorPicker { get; private set; }
/// <summary>
/// Gets or sets the <see cref="Style"/> for the <see cref="Controls.ColorPicker"/> control used in the button.
/// </summary>
public Style ColorPickerStyle
{
get
{
return (Style)GetValue(ColorPickerStyleProperty);
}
set
{
SetValue(ColorPickerStyleProperty, value);
}
}
/// <summary>
/// Identifies the <see cref="ColorPickerStyle"/> dependency property.
/// </summary>
public static readonly DependencyProperty ColorPickerStyleProperty = DependencyProperty.Register("ColorPickerStyle", typeof(Style), typeof(ColorPickerButton), new PropertyMetadata(default(Style)));
/// <summary>
/// Gets or sets the <see cref="Style"/> for the <see cref="FlyoutPresenter"/> used within the <see cref="Flyout"/> of the <see cref="DropDownButton"/>.
/// </summary>
public Style FlyoutPresenterStyle
{
get
{
return (Style)GetValue(FlyoutPresenterStyleProperty);
}
set
{
SetValue(FlyoutPresenterStyleProperty, value);
}
}
/// <summary>
/// Identifies the <see cref="FlyoutPresenterStyle"/> dependency property.
/// </summary>
public static readonly DependencyProperty FlyoutPresenterStyleProperty = DependencyProperty.Register("FlyoutPresenterStyle", typeof(Style), typeof(ColorPickerButton), new PropertyMetadata(default(Style)));
#pragma warning disable CS0419 // Ambiguous reference in cref attribute
/// <summary>
/// Gets or sets the selected <see cref="Windows.UI.Color"/> the user has picked from the <see cref="ColorPicker"/>.
/// </summary>
#pragma warning restore CS0419 // Ambiguous reference in cref attribute
public Color SelectedColor
{
get { return (Color)GetValue(SelectedColorProperty); }
set { SetValue(SelectedColorProperty, value); }
}
/// <summary>
/// Identifies the <see cref="SelectedColor"/> dependency property.
/// </summary>
public static readonly DependencyProperty SelectedColorProperty =
DependencyProperty.Register(nameof(SelectedColor), typeof(Color), typeof(ColorPickerButton), new PropertyMetadata(null));
#pragma warning disable SA1306 // Field names should begin with lower-case letter
//// Template Parts
private Border CheckeredBackgroundBorder;
#pragma warning restore SA1306 // Field names should begin with lower-case letter
/// <summary>
/// Initializes a new instance of the <see cref="ColorPickerButton"/> class.
/// </summary>
public ColorPickerButton()
{
this.DefaultStyleKey = typeof(ColorPickerButton);
}
/// <inheritdoc/>
protected override void OnApplyTemplate()
{
if (ColorPicker != null)
{
ColorPicker.ColorChanged -= ColorPicker_ColorChanged;
}
base.OnApplyTemplate();
if (ColorPickerStyle != null)
{
ColorPicker = new ColorPicker() { Style = ColorPickerStyle };
}
else
{
ColorPicker = new ColorPicker();
}
ColorPicker.Color = SelectedColor;
ColorPicker.ColorChanged += ColorPicker_ColorChanged;
if (Flyout == null)
{
Flyout = new Flyout()
{
// TODO: Expose Placement
Placement = Windows.UI.Xaml.Controls.Primitives.FlyoutPlacementMode.BottomEdgeAlignedLeft,
FlyoutPresenterStyle = FlyoutPresenterStyle,
Content = ColorPicker
};
}
if (CheckeredBackgroundBorder != null)
{
CheckeredBackgroundBorder.Loaded -= this.CheckeredBackgroundBorder_Loaded;
}
CheckeredBackgroundBorder = GetTemplateChild(nameof(CheckeredBackgroundBorder)) as Border;
if (CheckeredBackgroundBorder != null)
{
CheckeredBackgroundBorder.Loaded += this.CheckeredBackgroundBorder_Loaded;
}
}
private void ColorPicker_ColorChanged(Microsoft.UI.Xaml.Controls.ColorPicker sender, Microsoft.UI.Xaml.Controls.ColorChangedEventArgs args)
{
SelectedColor = args.NewColor;
}
private async void CheckeredBackgroundBorder_Loaded(object sender, RoutedEventArgs e)
{
await ColorPickerRenderingHelpers.UpdateBorderBackgroundWithCheckerAsync(
sender as Border,
ColorPicker.CheckerBackgroundColor); // TODO: Check initialization
}
}
}