Skip to content

Commit 9fb6ad9

Browse files
kubaflojsuarezruiz
authored andcommitted
Switch Thumb and On Colors
1 parent 3992a45 commit 9fb6ad9

File tree

3 files changed

+83
-2
lines changed

3 files changed

+83
-2
lines changed

src/Controls/src/Core/Switch/Switch.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public partial class Switch : View, IElementConfiguration<Switch>, ISwitch
1919
((Switch)bindable).Toggled?.Invoke(bindable, new ToggledEventArgs((bool)newValue));
2020
((Switch)bindable).ChangeVisualState();
2121
((IView)bindable)?.Handler?.UpdateValue(nameof(ISwitch.TrackColor));
22-
22+
((IView)bindable)?.Handler?.UpdateValue(nameof(ISwitch.ThumbColor));
2323
}, defaultBindingMode: BindingMode.TwoWay);
2424

2525
/// <summary>Bindable property for <see cref="OnColor"/>.</summary>
@@ -30,7 +30,11 @@ public partial class Switch : View, IElementConfiguration<Switch>, ISwitch
3030
});
3131

3232
/// <summary>Bindable property for <see cref="ThumbColor"/>.</summary>
33-
public static readonly BindableProperty ThumbColorProperty = BindableProperty.Create(nameof(ThumbColor), typeof(Color), typeof(Switch), null);
33+
public static readonly BindableProperty ThumbColorProperty = BindableProperty.Create(nameof(ThumbColor), typeof(Color), typeof(Switch), null,
34+
propertyChanged: (bindable, oldValue, newValue) =>
35+
{
36+
((IView)bindable)?.Handler?.UpdateValue(nameof(ISwitch.ThumbColor));
37+
});
3438

3539
/// <include file="../../docs/Microsoft.Maui.Controls/Switch.xml" path="//Member[@MemberName='OnColor']/Docs/*" />
3640
public Color OnColor
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
using Microsoft.Maui.Graphics;
2+
using NUnit.Framework;
3+
4+
namespace Microsoft.Maui.Controls.Xaml.UnitTests
5+
{
6+
public partial class Issue19380 : ContentPage
7+
{
8+
public Issue19380()
9+
{
10+
InitializeComponent();
11+
}
12+
13+
public Issue19380(bool useCompiledXaml)
14+
{
15+
//this stub will be replaced at compile time
16+
}
17+
18+
[TestFixture]
19+
class Tests
20+
{
21+
[TestCase(true)]
22+
[TestCase(false)]
23+
public void ShouldOverrideThumbAndOnColorsFromResources(bool useCompiledXaml)
24+
{
25+
var layout = new Issue19380(useCompiledXaml);
26+
27+
var switch1 = layout.Switch1;
28+
29+
Assert.True(switch1.OnColor == Colors.Red);
30+
Assert.True(switch1.ThumbColor == Colors.Blue);
31+
32+
switch1.IsToggled = true;
33+
34+
Assert.True(switch1.OnColor == Colors.Red);
35+
Assert.True(switch1.ThumbColor == Colors.Blue);
36+
}
37+
}
38+
}
39+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
3+
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
4+
x:Class="Microsoft.Maui.Controls.Xaml.UnitTests.Issue19380">
5+
6+
<ContentPage.Resources>
7+
<Style TargetType="Switch">
8+
<Setter Property="VisualStateManager.VisualStateGroups">
9+
<VisualStateGroupList>
10+
<VisualStateGroup x:Name="CommonStates">
11+
<VisualState x:Name="Normal" />
12+
<VisualState x:Name="Disabled">
13+
<VisualState.Setters>
14+
<Setter Property="OnColor" Value="Black" />
15+
<Setter Property="ThumbColor" Value="Black" />
16+
</VisualState.Setters>
17+
</VisualState>
18+
<VisualState x:Name="On">
19+
<VisualState.Setters>
20+
<Setter Property="OnColor" Value="Green" />
21+
<Setter Property="ThumbColor" Value="Green" />
22+
</VisualState.Setters>
23+
</VisualState>
24+
<VisualState x:Name="Off">
25+
<VisualState.Setters>
26+
<Setter Property="ThumbColor" Value="White" />
27+
</VisualState.Setters>
28+
</VisualState>
29+
</VisualStateGroup>
30+
</VisualStateGroupList>
31+
</Setter>
32+
</Style>
33+
</ContentPage.Resources>
34+
35+
<StackLayout>
36+
<Switch x:Name="Switch1" OnColor="Red" ThumbColor="Blue"/>
37+
</StackLayout>
38+
</ContentPage>

0 commit comments

Comments
 (0)