[WinRT/UWP] Apply BackgroundColor to Stepper buttons (#581)
authorPaul DiPietro <pauldipietro@users.noreply.github.com>
Tue, 3 Jan 2017 11:31:48 +0000 (05:31 -0600)
committerRui Marinho <me@ruimarinho.net>
Tue, 3 Jan 2017 11:31:48 +0000 (11:31 +0000)
* [WinRT/UWP] Apply BackgroundColor to Stepper buttons

* Add explanatory text; use nameof

* Move explanatory text to a label

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla48236.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.UAP/StepperControl.cs
Xamarin.Forms.Platform.WinRT/StepperControl.xaml.cs
Xamarin.Forms.Platform.WinRT/StepperRenderer.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 (file)
index 0000000..322d4fc
--- /dev/null
@@ -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
index 4820bae..e0af0ca 100644 (file)
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla46494.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla44476.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla46630.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Bugzilla48236.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla47971.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)CarouselAsync.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Bugzilla34561.cs" />
index 0de0931..6cf4108 100644 (file)
@@ -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;
index 49a4632..bf83bf1 100644 (file)
@@ -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;
index 7a4724c..4f09b12 100644 (file)
@@ -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)