From 6fc18e058bc5bf7eb645fcd91ffdaf6974dfb375 Mon Sep 17 00:00:00 2001 From: Paul DiPietro Date: Tue, 3 Jan 2017 05:31:48 -0600 Subject: [PATCH] [WinRT/UWP] Apply BackgroundColor to Stepper buttons (#581) * [WinRT/UWP] Apply BackgroundColor to Stepper buttons * Add explanatory text; use nameof * Move explanatory text to a label --- .../Bugzilla48236.cs | 46 ++++++++++++++++++++++ .../Xamarin.Forms.Controls.Issues.Shared.projitems | 1 + Xamarin.Forms.Platform.UAP/StepperControl.cs | 26 ++++++++++++ .../StepperControl.xaml.cs | 21 ++++++++++ Xamarin.Forms.Platform.WinRT/StepperRenderer.cs | 8 ++++ 5 files changed, 102 insertions(+) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla48236.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla48236.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla48236.cs new file mode 100644 index 0000000..322d4fc --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla48236.cs @@ -0,0 +1,46 @@ +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; + +#if UITEST +using Xamarin.UITest; +using NUnit.Framework; +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 48236, "[WinRT/UWP] BackgroundColor for Stepper behaves differently compared to iOS to Android", PlatformAffected.WinRT)] + public class Bugzilla48236 : TestContentPage + { + protected override void Init() + { + var stepper = new Stepper + { + BackgroundColor = Color.Green, + Minimum = 0, + Maximum = 10 + }; + + Content = new StackLayout + { + Children = + { + new Label + { + Text = "If the Stepper's background color extends the width of the page, then this test has failed." + }, + stepper, + new Button + { + BackgroundColor = Color.Aqua, + Text = "Change Stepper Color to Yellow", + Command = new Command(() => + { + stepper.BackgroundColor = Color.Yellow; + }) + } + } + }; + } + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 4820bae..e0af0ca 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -152,6 +152,7 @@ + diff --git a/Xamarin.Forms.Platform.UAP/StepperControl.cs b/Xamarin.Forms.Platform.UAP/StepperControl.cs index 0de0931..6cf4108 100644 --- a/Xamarin.Forms.Platform.UAP/StepperControl.cs +++ b/Xamarin.Forms.Platform.UAP/StepperControl.cs @@ -18,6 +18,8 @@ namespace Xamarin.Forms.Platform.UWP public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(double), typeof(StepperControl), new PropertyMetadata(default(double), OnIncrementChanged)); + public static readonly DependencyProperty ButtonBackgroundColorProperty = DependencyProperty.Register(nameof(ButtonBackgroundColor), typeof(Color), typeof(StepperControl), new PropertyMetadata(default(Color), OnButtonBackgroundColorChanged)); + Windows.UI.Xaml.Controls.Button _plus; Windows.UI.Xaml.Controls.Button _minus; VisualStateCache _plusStateCache; @@ -52,6 +54,12 @@ namespace Xamarin.Forms.Platform.UWP set { SetValue(ValueProperty, value); } } + public Color ButtonBackgroundColor + { + get { return (Color)GetValue(ButtonBackgroundColorProperty); } + set { SetValue(ButtonBackgroundColorProperty, value); } + } + public event EventHandler ValueChanged; protected override void OnApplyTemplate() @@ -67,6 +75,13 @@ namespace Xamarin.Forms.Platform.UWP _minus.Click += OnMinusClicked; UpdateEnabled(Value); + UpdateButtonBackgroundColor(ButtonBackgroundColor); + } + + static void OnButtonBackgroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var stepper = (StepperControl)d; + stepper.UpdateButtonBackgroundColor(stepper.ButtonBackgroundColor); } static void OnIncrementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) @@ -189,6 +204,17 @@ namespace Xamarin.Forms.Platform.UWP cache = null; } + void UpdateButtonBackgroundColor(Color value) + { + Brush brush = value.ToBrush(); + _minus = GetTemplateChild("Minus") as Windows.UI.Xaml.Controls.Button; + _plus = GetTemplateChild("Plus") as Windows.UI.Xaml.Controls.Button; + if (_minus != null) + _minus.Background = brush; + if (_plus != null) + _plus.Background = brush; + } + void UpdateEnabled(double value) { double increment = Increment; diff --git a/Xamarin.Forms.Platform.WinRT/StepperControl.xaml.cs b/Xamarin.Forms.Platform.WinRT/StepperControl.xaml.cs index 49a4632..bf83bf1 100644 --- a/Xamarin.Forms.Platform.WinRT/StepperControl.xaml.cs +++ b/Xamarin.Forms.Platform.WinRT/StepperControl.xaml.cs @@ -15,6 +15,8 @@ namespace Xamarin.Forms.Platform.WinRT public static readonly DependencyProperty IncrementProperty = DependencyProperty.Register("Increment", typeof(double), typeof(StepperControl), new PropertyMetadata(default(double), OnIncrementChanged)); + public static readonly DependencyProperty ButtonBackgroundColorProperty = DependencyProperty.Register(nameof(ButtonBackgroundColor), typeof(Color), typeof(StepperControl), new PropertyMetadata(default(Color), OnButtonBackgroundColorChanged)); + public StepperControl() { InitializeComponent(); @@ -44,8 +46,20 @@ namespace Xamarin.Forms.Platform.WinRT set { SetValue(ValueProperty, value); } } + public Color ButtonBackgroundColor + { + get { return (Color)GetValue(ButtonBackgroundColorProperty); } + set { SetValue(ButtonBackgroundColorProperty, value); } + } + public event EventHandler ValueChanged; + static void OnButtonBackgroundColorChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + var stepper = (StepperControl)d; + stepper.UpdateButtonBackgroundColor(stepper.ButtonBackgroundColor); + } + static void OnIncrementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { var stepper = (StepperControl)d; @@ -78,6 +92,13 @@ namespace Xamarin.Forms.Platform.WinRT changed(d, EventArgs.Empty); } + void UpdateButtonBackgroundColor(Color value) + { + Windows.UI.Xaml.Media.Brush brush = value.ToBrush(); + Minus.Background = brush; + Plus.Background = brush; + } + void UpdateEnabled(double value) { double increment = Increment; diff --git a/Xamarin.Forms.Platform.WinRT/StepperRenderer.cs b/Xamarin.Forms.Platform.WinRT/StepperRenderer.cs index 7a4724c..4f09b12 100644 --- a/Xamarin.Forms.Platform.WinRT/StepperRenderer.cs +++ b/Xamarin.Forms.Platform.WinRT/StepperRenderer.cs @@ -42,6 +42,14 @@ namespace Xamarin.Forms.Platform.WinRT UpdateMinimum(); else if (e.PropertyName == Stepper.IncrementProperty.PropertyName) UpdateIncrement(); + else if (e.PropertyName == VisualElement.BackgroundColorProperty.PropertyName) + UpdateBackgroundColor(); + } + + protected override void UpdateBackgroundColor() + { + if (Control != null) + Control.ButtonBackgroundColor = Element.BackgroundColor; } void OnControlValue(object sender, EventArgs e) -- 2.7.4