Implemented Padding on WPF Button (#6791)
authorGerald Versluis <github@geraldversluis.nl>
Tue, 9 Jul 2019 21:46:35 +0000 (23:46 +0200)
committerSamantha Houts <samhouts@users.noreply.github.com>
Tue, 9 Jul 2019 21:46:35 +0000 (14:46 -0700)
* Implemented Padding on WPF Button

* Update Padding when BorderWidth is updated

* Implemented Padding for ImageButton

* Apply suggestions from code review

Co-Authored-By: Pavel Yakovlev <v-payako@microsoft.com>
fixes #6790

Xamarin.Forms.Controls/CoreGalleryPages/ImageButtonCoreGalleryPage.cs
Xamarin.Forms.CustomAttributes/TestAttributes.cs
Xamarin.Forms.Platform.WPF/Renderers/ButtonRenderer.cs
Xamarin.Forms.Platform.WPF/Renderers/ImageButtonRenderer.cs

index 0b51bf5..0dec344 100644 (file)
@@ -94,6 +94,15 @@ namespace Xamarin.Forms.Controls
                                }
                        );
 
+                       var paddingContainer = new ViewContainer<ImageButton>(Test.ImageButton.Padding,
+                               new ImageButton
+                               {
+                                       Source = "oasissmall.jpg",
+                                       BackgroundColor = Color.Red,
+                                       Padding = new Thickness(20, 30, 60, 15)
+                               }
+                       );
+
 
                        InitializeElement(aspectFillContainer.View);
                        InitializeElement(aspectFitContainer.View);
@@ -117,6 +126,7 @@ namespace Xamarin.Forms.Controls
                        Add(corderRadiusContainer);
                        Add(imageContainer);
                        Add(pressedContainer);
+                       Add(paddingContainer);
                }
        }
 }
\ No newline at end of file
index c609b42..7e7d9f1 100644 (file)
@@ -570,7 +570,8 @@ namespace Xamarin.Forms.CustomAttributes
                        Clicked,
                        Command,
                        Image,
-                       Pressed
+                       Pressed,
+                       Padding
                }
 
                public enum ImageSource
index 9c6ab1c..20b8b71 100644 (file)
@@ -36,6 +36,9 @@ namespace Xamarin.Forms.Platform.WPF
                                if (Element.BorderWidth != 0)
                                        UpdateBorderWidth();
 
+                               if (Element.IsSet(Button.PaddingProperty))
+                                       UpdatePadding();
+
                                UpdateFont();
                        }
 
@@ -55,7 +58,12 @@ namespace Xamarin.Forms.Platform.WPF
                        else if (e.PropertyName == Button.BorderColorProperty.PropertyName)
                                UpdateBorderColor();
                        else if (e.PropertyName == Button.BorderWidthProperty.PropertyName)
+                       {
                                UpdateBorderWidth();
+                               UpdatePadding();
+                       }
+                       else if (e.PropertyName == Button.PaddingProperty.PropertyName)
+                               UpdatePadding();
                }
 
                void HandleButtonClick(object sender, RoutedEventArgs e)
@@ -185,5 +193,15 @@ namespace Xamarin.Forms.Platform.WPF
                        _isDisposed = true;
                        base.Dispose(disposing);
                }
+
+               void UpdatePadding()
+               {
+                       Control.Padding = new WThickness(
+                               Element.Padding.Left,
+                               Element.Padding.Top,
+                               Element.Padding.Right,
+                               Element.Padding.Bottom
+                       );
+               }
        }
-}
\ No newline at end of file
+}
index 03a4335..1beda9a 100644 (file)
@@ -55,6 +55,9 @@ namespace Xamarin.Forms.Platform.WPF
 
                                await TryUpdateSource().ConfigureAwait(false);
                                UpdateAspect();
+
+                               if (Element.IsSet(Button.PaddingProperty))
+                                       UpdatePadding();
                        }
                }
 
@@ -67,9 +70,14 @@ namespace Xamarin.Forms.Platform.WPF
                        else if (e.PropertyName == ImageButton.BorderColorProperty.PropertyName)
                                UpdateBorderColor();
                        else if (e.PropertyName == ImageButton.BorderWidthProperty.PropertyName)
+                       {
                                UpdateBorderWidth();
+                               UpdatePadding();
+                       }
                        else if (e.PropertyName == ImageButton.AspectProperty.PropertyName)
                                UpdateAspect();
+                       else if (e.PropertyName == Button.PaddingProperty.PropertyName)
+                               UpdatePadding();
                }
 
                protected override void Dispose(bool disposing)
@@ -190,5 +198,15 @@ namespace Xamarin.Forms.Platform.WPF
                                Control.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                        }
                }
+
+               void UpdatePadding()
+               {
+                       Control.Padding = new WThickness(
+                               Element.Padding.Left,
+                               Element.Padding.Top,
+                               Element.Padding.Right,
+                               Element.Padding.Bottom
+                       );
+               }
        }
 }