Fix Button.CornerRadius doesn't work on WPF (#7895)
authormelimion <33512073+melimion@users.noreply.github.com>
Tue, 5 Nov 2019 22:37:43 +0000 (01:37 +0300)
committerSamantha Houts <samhouts@users.noreply.github.com>
Tue, 5 Nov 2019 22:37:43 +0000 (14:37 -0800)
* fix

* test added

* button without corner radius added to test
fixes #6556

Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6556.cs [new file with mode: 0644]
Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems
Xamarin.Forms.Platform.WPF/Controls/FormsButton.cs [new file with mode: 0644]
Xamarin.Forms.Platform.WPF/Renderers/ButtonRenderer.cs
Xamarin.Forms.Platform.WPF/Xamarin.Forms.Platform.WPF.csproj

diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6556.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Issue6556.cs
new file mode 100644 (file)
index 0000000..bec6a7d
--- /dev/null
@@ -0,0 +1,40 @@
+using Xamarin.Forms.CustomAttributes;
+using Xamarin.Forms.Internals;
+
+
+namespace Xamarin.Forms.Controls.Issues
+{
+       [Preserve(AllMembers = true)]
+       [Issue(IssueTracker.Github, 6556, "Button.CornerRadius doesn't work on WPF", PlatformAffected.WPF)]
+       public class Issue6556 : TestContentPage
+       {
+               protected override void Init()
+               {
+                       var sl = new StackLayout { Padding = new Size(20, 20) };
+                       sl.Children.Add(new TestButton(0));
+                       sl.Children.Add(new TestButton(5));
+                       sl.Children.Add(new TestButton(10));
+                       sl.Children.Add(new TestButton(20));
+                       sl.Children.Add(new Button()
+                       {
+                               Text = "Round",
+                               WidthRequest = 50,
+                               HeightRequest = 50,
+                               CornerRadius = 25,
+                               BorderWidth = 5,
+                               HorizontalOptions = LayoutOptions.Center
+                       });
+                       Content = sl;
+               }
+
+               class TestButton : Button
+               {
+                       public TestButton(int cr)
+                       {
+                               Text = $"radius is {cr}";
+                               BorderWidth = 5;
+                               CornerRadius = cr;
+                       }
+               }
+       }
+}
\ No newline at end of file
index 64c938d..abaa502 100644 (file)
@@ -22,6 +22,7 @@
     <Compile Include="$(MSBuildThisFileDirectory)Issue4606.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue8186.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue3475.cs" />
+    <Compile Include="$(MSBuildThisFileDirectory)Issue6556.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue5830.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue6476.cs" />
     <Compile Include="$(MSBuildThisFileDirectory)Issue7396.cs" />
diff --git a/Xamarin.Forms.Platform.WPF/Controls/FormsButton.cs b/Xamarin.Forms.Platform.WPF/Controls/FormsButton.cs
new file mode 100644 (file)
index 0000000..f859756
--- /dev/null
@@ -0,0 +1,47 @@
+using System.Linq;
+using System.Windows;
+using System.Windows.Controls;
+using WButton = System.Windows.Controls.Button;
+
+namespace Xamarin.Forms.Platform.WPF.Controls
+{
+       public class FormsButton : WButton
+       {
+               public static readonly DependencyProperty CornerRadiusProperty = DependencyProperty.Register(nameof(CornerRadius), typeof(int), typeof(FormsButton),
+                       new PropertyMetadata(default(int), OnCornerRadiusChanged));
+
+               Border _contentPresenter;
+
+
+               public int CornerRadius
+               {
+                       get
+                       {
+                               return (int)GetValue(CornerRadiusProperty);
+                       }
+                       set
+                       {
+                               SetValue(CornerRadiusProperty, value);
+                       }
+               }
+
+               public override void OnApplyTemplate()
+               {
+                       base.OnApplyTemplate();
+
+                       _contentPresenter = this.GetChildren<Border>().FirstOrDefault();
+                       UpdateCornerRadius();
+               }
+
+               static void OnCornerRadiusChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
+               {
+                       ((FormsButton)d).UpdateCornerRadius();
+               }
+
+               void UpdateCornerRadius()
+               {
+                       if (_contentPresenter != null)
+                               _contentPresenter.CornerRadius = new System.Windows.CornerRadius(CornerRadius);
+               }
+       }
+}
index 20b8b71..50af957 100644 (file)
@@ -5,13 +5,14 @@ using System.Windows.Controls;
 using System.Windows.Media;
 using System.Windows.Media.Imaging;
 using Xamarin.Forms.Internals;
+using Xamarin.Forms.Platform.WPF.Controls;
 using WButton = System.Windows.Controls.Button;
 using WImage = System.Windows.Controls.Image;
 using WThickness = System.Windows.Thickness;
 
 namespace Xamarin.Forms.Platform.WPF
 {
-       public class ButtonRenderer : ViewRenderer<Button, WButton>
+       public class ButtonRenderer : ViewRenderer<Button, FormsButton>
        {
                bool _fontApplied;
 
@@ -21,7 +22,7 @@ namespace Xamarin.Forms.Platform.WPF
                        {
                                if (Control == null) // construct and SetNativeControl and suscribe control event
                                {
-                                       SetNativeControl(new WButton());
+                                       SetNativeControl(new FormsButton());
                                        Control.Click += HandleButtonClick;
                                }
 
@@ -33,9 +34,12 @@ namespace Xamarin.Forms.Platform.WPF
                                if (Element.BorderColor != Color.Default)
                                        UpdateBorderColor();
 
-                               if (Element.BorderWidth != 0)
+                               if (Element.IsSet(Button.BorderWidthProperty) && Element.BorderWidth != (double)Button.BorderWidthProperty.DefaultValue)
                                        UpdateBorderWidth();
 
+                               if (Element.IsSet(Button.CornerRadiusProperty) && Element.CornerRadius != (int)Button.CornerRadiusProperty.DefaultValue)
+                                       UpdateCornerRadius();
+                                       
                                if (Element.IsSet(Button.PaddingProperty))
                                        UpdatePadding();
 
@@ -62,6 +66,8 @@ namespace Xamarin.Forms.Platform.WPF
                                UpdateBorderWidth();
                                UpdatePadding();
                        }
+                       else if (e.PropertyName == Button.CornerRadiusProperty.PropertyName)
+                               UpdateCornerRadius();
                        else if (e.PropertyName == Button.PaddingProperty.PropertyName)
                                UpdatePadding();
                }
@@ -82,6 +88,10 @@ namespace Xamarin.Forms.Platform.WPF
                {
                        Control.BorderThickness = Element.BorderWidth <= 0d ? new WThickness(1) : new WThickness(Element.BorderWidth);
                }
+               void UpdateCornerRadius()
+               {
+                       Control.CornerRadius = Element.CornerRadius;
+               }
 
                async void UpdateContent()
                {
index cea097e..cf9cb04 100644 (file)
@@ -65,6 +65,7 @@
     <Compile Include="Controls\FormsAppBar.cs" />
     <Compile Include="Controls\FormsAppBarButton.cs" />
     <Compile Include="Controls\FormsBitmapIcon.cs" />
+    <Compile Include="Controls\FormsButton.cs" />
     <Compile Include="Controls\FormsCarouselPage.cs" />
     <Compile Include="Controls\FormsContentControl.cs" />
     <Compile Include="Controls\FormsContentDialog.cs" />