<ContentPage.Resources>
<ResourceDictionary>
- <local:ThicknessConverter x:Key="ThicknessConverter" />
+ <local:NullableThicknessConverter x:Key="NullableThicknessConverter" />
<Style TargetType="Label">
<Setter Property="FontSize" Value="12" />
</Style>
<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">
string _buttonText = "Text";
string _buttonImage = "bank.png";
- Thickness _buttonPadding = default(Thickness);
+ Thickness? _buttonPadding;
double _buttonFontSize = -1.0;
}
}
- 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);
+ }
}
}
namespace Xamarin.Forms.Controls
{
- public class ThicknessConverter : IValueConverter
+ public class NullableThicknessConverter : IValueConverter
{
ThicknessTypeConverter _converter = new ThicknessTypeConverter();
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;
// no-op
}
- return default(Thickness);
+ return null;
}
}
}