Skip to content

Commit

Permalink
Allow string property binding as Color source (dotnet#9814) Fixes dot…
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuarezruiz authored Mar 2, 2023
1 parent 7f3b34f commit c5d9e5f
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@
<Button
BackgroundColor="Blue"
Text="Button"/>
<Label
Text="BackgroundColor (Binding)"
Style="{StaticResource Headline}" />
<Button
Text="Button"
BackgroundColor="{Binding ButtonBackground}" />
<Label
Text="Background"
Style="{StaticResource Headline}" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ void OnBackgroundButtonClicked(object sender, System.EventArgs e)

public class ButtonPageViewModel : BindableObject
{
public string ButtonBackground => "#fc87ad";

public ICommand ButtonCommand => new Command(OnExecuteImageButtonCommand);

void OnExecuteImageButtonCommand()
Expand Down
1 change: 1 addition & 0 deletions src/Controls/src/Core/BindableProperty.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public sealed class BindableProperty
{
{ typeof(Uri), new UriTypeConverter() },
{ typeof(Easing), new Maui.Converters.EasingTypeConverter() },
{ typeof(Maui.Graphics.Color), new ColorTypeConverter() },
};

static readonly Dictionary<Type, IValueConverter> KnownIValueConverters = new Dictionary<Type, IValueConverter>
Expand Down
12 changes: 12 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/ColorConverter.xaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.ColorConverter">
<ContentPage.Content>
<Button
x:Name="Button0"
Text="Button"
BackgroundColor="{Binding ButtonBackground}" />
</ContentPage.Content>
</ContentPage>
39 changes: 39 additions & 0 deletions src/Controls/tests/Xaml.UnitTests/ColorConverter.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
using Microsoft.Maui.Controls.Core.UnitTests;
using Microsoft.Maui.Graphics;
using NUnit.Framework;

namespace Microsoft.Maui.Controls.Xaml.UnitTests
{
public partial class ColorConverter : ContentPage
{
class Data
{
public string ButtonBackground => "#fc87ad";
}

public ColorConverter()
{
InitializeComponent();
}

public ColorConverter(bool useCompiledXaml)
{
//this stub will be replaced at compile time
}

[TestFixture]
public class Tests
{
[TestCase(false)]
[TestCase(true)]
public void StringsAreValidAsColor(bool useCompiledXaml)
{
var page = new ColorConverter(useCompiledXaml);
page.BindingContext = new Data();

var expected = Color.FromArgb("#fc87ad");
Assert.AreEqual(expected, page.Button0.BackgroundColor);
}
}
}
}

0 comments on commit c5d9e5f

Please sign in to comment.