Button Layout gallery page: don't set initial Padding (#6599) fixes #6469
authorAndrei Nitescu <nitescua@yahoo.com>
Thu, 27 Jun 2019 10:42:42 +0000 (13:42 +0300)
committerRui Marinho <me@ruimarinho.net>
Thu, 27 Jun 2019 10:42:42 +0000 (11:42 +0100)
Xamarin.Forms.Controls/GalleryPages/ButtonLayoutGalleryPage.xaml
Xamarin.Forms.Controls/GalleryPages/ButtonLayoutGalleryPage.xaml.cs
Xamarin.Forms.Controls/NullableThicknessConverter.cs [moved from Xamarin.Forms.Controls/ThicknessConverter.cs with 83% similarity]

index 1b7417d..533db72 100644 (file)
@@ -6,7 +6,7 @@
 
        <ContentPage.Resources>
                <ResourceDictionary>
-                       <local:ThicknessConverter x:Key="ThicknessConverter" />
+            <local:NullableThicknessConverter x:Key="NullableThicknessConverter" />
                        <Style TargetType="Label">
                                <Setter Property="FontSize" Value="12" />
                        </Style>
@@ -32,7 +32,7 @@
 
                        <Label Text="Autosized:" Margin="0,0,0,-5" />
                        <StackLayout Orientation="Horizontal" Padding="0" Spacing="5">
-                               <Button x:Name="autosizedButton" Text="{Binding ButtonText}" Image="{Binding ButtonImage}" Padding="{Binding ButtonPadding}" ContentLayout="{Binding ButtonImageLayout}" FontSize="{Binding ButtonFontSize}" />
+                               <Button x:Name="autosizedButton" Text="{Binding ButtonText}" Image="{Binding ButtonImage}" ContentLayout="{Binding ButtonImageLayout}" FontSize="{Binding ButtonFontSize}" />
                        </StackLayout>
 
                        <Label Text="Explicit Size:" Margin="0,0,0,-5" />
                                </Grid.ColumnDefinitions>
 
                                <Button
-                                       x:Name="explicitButton" Text="{Binding ButtonText}" Image="{Binding ButtonImage}" Padding="{Binding ButtonPadding}" ContentLayout="{Binding ButtonImageLayout}" FontSize="{Binding ButtonFontSize}"
+                                       x:Name="explicitButton" Text="{Binding ButtonText}" Image="{Binding ButtonImage}" ContentLayout="{Binding ButtonImageLayout}" FontSize="{Binding ButtonFontSize}"
                                        WidthRequest="120" HeightRequest="60" HorizontalOptions="Start" VerticalOptions="Start" />
                                <Button
-                                       x:Name="explicitWidthButton" Text="{Binding ButtonText}" Image="{Binding ButtonImage}" Padding="{Binding ButtonPadding}" ContentLayout="{Binding ButtonImageLayout}" FontSize="{Binding ButtonFontSize}"
+                                       x:Name="explicitWidthButton" Text="{Binding ButtonText}" Image="{Binding ButtonImage}" ContentLayout="{Binding ButtonImageLayout}" FontSize="{Binding ButtonFontSize}"
                                        Grid.Column="1" Grid.RowSpan="2" WidthRequest="120" HorizontalOptions="Start" VerticalOptions="Start" />
                                <Button
-                                       x:Name="explicitHeightButton" Text="{Binding ButtonText}" Image="{Binding ButtonImage}" Padding="{Binding ButtonPadding}" ContentLayout="{Binding ButtonImageLayout}" FontSize="{Binding ButtonFontSize}"
+                                       x:Name="explicitHeightButton" Text="{Binding ButtonText}" Image="{Binding ButtonImage}" ContentLayout="{Binding ButtonImageLayout}" FontSize="{Binding ButtonFontSize}"
                                        Grid.Row="1" Grid.ColumnSpan="2" HeightRequest="60" HorizontalOptions="Start" VerticalOptions="Start" />
                        </Grid>
 
                        <Label Text="Stretched:" Margin="0,0,0,-5" />
                        <StackLayout Orientation="Vertical" Padding="0" Spacing="5">
-                               <Button x:Name="stretchedButton" Text="{Binding ButtonText}" Image="{Binding ButtonImage}" Padding="{Binding ButtonPadding}" ContentLayout="{Binding ButtonImageLayout}" FontSize="{Binding ButtonFontSize}" />
+                               <Button x:Name="stretchedButton" Text="{Binding ButtonText}" Image="{Binding ButtonImage}" ContentLayout="{Binding ButtonImageLayout}" FontSize="{Binding ButtonFontSize}" />
                        </StackLayout>
 
                        <Label Text="Options:" Margin="0,5,0,0" FontAttributes="Bold" />
 
                                <StackLayout Orientation="Vertical" Padding="0" Spacing="0" HorizontalOptions="FillAndExpand">
                                        <Label Text="Padding:" />
-                                       <Entry Text="{Binding ButtonPadding, Converter={StaticResource ThicknessConverter}, Mode=OneWayToSource, TargetNullValue='0, 0, 0, 0'}" HorizontalOptions="FillAndExpand" />
+                    <Entry Text="{Binding ButtonPadding, Converter={StaticResource NullableThicknessConverter}, Mode=OneWayToSource, TargetNullValue='0, 0, 0, 0'}" HorizontalOptions="FillAndExpand" />
                                </StackLayout>
 
                                <StackLayout Orientation="Vertical" Padding="0" Spacing="0">
index 1bac86c..ef1eca5 100644 (file)
@@ -17,7 +17,7 @@ namespace Xamarin.Forms.Controls
                string _buttonText = "Text";
                string _buttonImage = "bank.png";
 
-               Thickness _buttonPadding = default(Thickness);
+               Thickness? _buttonPadding;
 
                double _buttonFontSize = -1.0;
 
@@ -98,13 +98,24 @@ namespace Xamarin.Forms.Controls
                        }
                }
 
-               public Thickness ButtonPadding
+               public Thickness? ButtonPadding
                {
                        get => _buttonPadding;
                        set
                        {
+                               if (Equals(_buttonPadding, value))
+                                       return;
+
                                _buttonPadding = value;
                                OnPropertyChanged();
+
+                               foreach (var button in _buttons)
+                               {
+                                       if (_buttonPadding != null)
+                                               button.Padding = _buttonPadding.Value;
+                                       else
+                                               button.ClearValue(Button.PaddingProperty);
+                               }
                        }
                }
 
@@ -3,7 +3,7 @@ using System.Globalization;
 
 namespace Xamarin.Forms.Controls
 {
-       public class ThicknessConverter : IValueConverter
+       public class NullableThicknessConverter : IValueConverter
        {
                ThicknessTypeConverter _converter = new ThicknessTypeConverter();
 
@@ -12,11 +12,14 @@ namespace Xamarin.Forms.Controls
                        if (value is Thickness thickness)
                                return $"{thickness.Left}, {thickness.Top}, {thickness.Right}, {thickness.Bottom}";
 
-                       return "0, 0, 0, 0";
+                       return string.Empty;
                }
 
                public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
                {
+                       if (value is null)
+                               return null;
+
                        if (value is Thickness thickness)
                                return thickness;
 
@@ -30,7 +33,7 @@ namespace Xamarin.Forms.Controls
                                // no-op
                        }
 
-                       return default(Thickness);
+                       return null;
                }
        }
 }