Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Bind RadioButton default template root properties to RadioButton's pr… #12742

Merged
merged 2 commits into from
Nov 11, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions Xamarin.Forms.Core.UnitTests/RadioButtonTemplateTests.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
using System;
using System.Collections;
using NUnit.Framework;

namespace Xamarin.Forms.Core.UnitTests
{
[TestFixture(Category = "RadioButton")]
public class RadioButtonTemplateTests : BaseTestFixture
{
class FrameStyleCases : IEnumerable
{
public IEnumerator GetEnumerator()
{
yield return new object[] { Frame.VerticalOptionsProperty, LayoutOptions.End };
yield return new object[] { Frame.HorizontalOptionsProperty, LayoutOptions.End };
yield return new object[] { Frame.BackgroundColorProperty, Color.Red };
yield return new object[] { Frame.BorderColorProperty, Color.Magenta };
yield return new object[] { Frame.MarginProperty, new Thickness(1, 2, 3, 4) };
yield return new object[] { Frame.OpacityProperty, 0.67 };
yield return new object[] { Frame.RotationProperty, 0.3 };
yield return new object[] { Frame.ScaleProperty, 0.8 };
yield return new object[] { Frame.ScaleXProperty, 0.9 };
yield return new object[] { Frame.ScaleYProperty, 0.95 };
yield return new object[] { Frame.TranslationXProperty, 123 };
yield return new object[] { Frame.TranslationYProperty, 321 };
}
}

[TestCaseSource(typeof(FrameStyleCases))]
[Description("Frame Style properties should not affect RadioButton")]
public void RadioButtonIgnoresFrameStyleProperties(BindableProperty property, object value)
{
var implicitFrameStyle = new Style(typeof(Frame));
implicitFrameStyle.Setters.Add(new Setter() { Property = property, Value = value });

var page = new ContentPage();
page.Resources.Add(implicitFrameStyle);

var radioButton = new RadioButton() { ControlTemplate = RadioButton.DefaultTemplate };
page.Content = radioButton;

var root = (radioButton as IControlTemplated)?.TemplateRoot as Frame;

Assert.IsNotNull(root);
Assert.That(root.GetValue(property), Is.Not.EqualTo(value), $"{property.PropertyName} should be ignored.");
}

class RadioButtonStyleCases : IEnumerable
{
public IEnumerator GetEnumerator()
{
yield return new object[] { RadioButton.VerticalOptionsProperty, LayoutOptions.End };
yield return new object[] { RadioButton.HorizontalOptionsProperty, LayoutOptions.End };
yield return new object[] { RadioButton.BackgroundColorProperty, Color.Red };
yield return new object[] { RadioButton.MarginProperty, new Thickness(1, 2, 3, 4) };
yield return new object[] { RadioButton.OpacityProperty, 0.67 };
yield return new object[] { RadioButton.RotationProperty, 0.3 };
yield return new object[] { RadioButton.ScaleProperty, 0.8};
yield return new object[] { RadioButton.ScaleXProperty, 0.9 };
yield return new object[] { RadioButton.ScaleYProperty, 0.95 };
yield return new object[] { RadioButton.TranslationXProperty, 123 };
yield return new object[] { RadioButton.TranslationYProperty, 321 };
}
}

[TestCaseSource(typeof(RadioButtonStyleCases))]
[Description("RadioButton Style properties should affect RadioButton")]
public void RadioButtonStyleSetsPropertyOnTemplateRoot(BindableProperty property, object value)
{
var radioButtonStyle = new Style(typeof(RadioButton));
radioButtonStyle.Setters.Add(new Setter() { Property = property, Value = value });

var radioButton = new RadioButton() { ControlTemplate = RadioButton.DefaultTemplate, Style = radioButtonStyle };

var root = (radioButton as IControlTemplated)?.TemplateRoot as Frame;

Assert.IsNotNull(root);
Assert.That(root.GetValue(property), Is.EqualTo(value), $"{property.PropertyName} should match.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -289,4 +289,4 @@
<Copy SourceFiles="@(_NUnitTestAdapterFiles)" DestinationFolder="$(SolutionDir)packages\NUnitTestAdapter.AnyVersion\tools\%(RecursiveDir)" ContinueOnError="true" Retries="0" />
<Copy SourceFiles="@(_NUnitTestAdapterFiles)" DestinationFolder="$(SolutionDir)packages\NUnitTestAdapter.AnyVersion\build\%(RecursiveDir)" ContinueOnError="true" Retries="0" />
</Target>
</Project>
</Project>
19 changes: 14 additions & 5 deletions Xamarin.Forms.Core/RadioButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -417,18 +417,27 @@ void HandleRadioButtonGroupValueChanged(Layout<View> layout, RadioButtonGroupVal
IsChecked = true;
}

static void BindToTemplatedParent(BindableObject bindableObject, params BindableProperty[] properties)
{
foreach (var property in properties)
{
bindableObject.SetBinding(property, new Binding(property.PropertyName,
source: RelativeBindingSource.TemplatedParent));
}
}

static View BuildDefaultTemplate()
{
var frame = new Frame
{
HasShadow = false,
BackgroundColor = Color.Transparent,
VerticalOptions = LayoutOptions.Start,
HorizontalOptions = LayoutOptions.Start,
Margin = new Thickness(6),
Padding = new Thickness(0)
Padding = 6
};

BindToTemplatedParent(frame, BackgroundColorProperty, Frame.BorderColorProperty, HorizontalOptionsProperty,
MarginProperty, OpacityProperty, RotationProperty, ScaleProperty, ScaleXProperty, ScaleYProperty,
TranslationYProperty, TranslationXProperty, VerticalOptionsProperty);

var grid = new Grid
{
RowSpacing = 0,
Expand Down