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

Bug/9106 - Adding cascading fallbacks for visual state management in CheckBox #11609

Closed
wants to merge 4 commits into from
Closed
Changes from 1 commit
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
Prev Previous commit
Next Next commit
provide some fallbacks for when a user doesn't define all common states
  • Loading branch information
mjmostachetti committed Jul 30, 2020

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
commit ef8d2d8cd9c2ba75785a499a86d8287b764350b6
Original file line number Diff line number Diff line change
@@ -4,11 +4,13 @@
xmlns:local="clr-namespace:Xamarin.Forms.Controls"
x:Class="Xamarin.Forms.Controls.Issues.Issue9106">
<StackLayout Orientation="Vertical">
<Label Text="Although we did not define a visual state for IsChecked, we return to Normal state when IsChecked is true and we toggle IsEnabled from false to true."/>
<Label Text="If the user forgets to assign IsChecked or Normal for a visual state group but did define Disabled, the Visual State Manager should remove the Disabled Setters when trying to return to a Normal State."/>
<Button Text="Toggle IsEnabled" Command="{Binding ToggleCheckboxIsEnabledCommand}"/>
<CheckBox x:Name="CheckBoxUnderTest"
IsChecked="{Binding CheckboxIsChecked}"
IsEnabled="{Binding CheckboxIsEnabled}">

<!--Try different combinations of setting visual states-->
<VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="CommonStates">
<VisualState x:Name="Normal">
5 changes: 3 additions & 2 deletions Xamarin.Forms.Core/CheckBox.cs
Original file line number Diff line number Diff line change
@@ -37,10 +37,11 @@ protected internal override void ChangeVisualState()
if (IsEnabled && IsChecked)
{
if (!VisualStateManager.GoToState(this, IsCheckedVisualState))
VisualStateManager.GoToState(this, "Normal");
if(!VisualStateManager.GoToState(this, VisualStateManager.CommonStates.Normal))
VisualStateManager.ResetCommonStatesGroupSetters(this);
}
else
{
{
base.ChangeVisualState();
}
}
3 changes: 2 additions & 1 deletion Xamarin.Forms.Core/VisualElement.cs
Original file line number Diff line number Diff line change
@@ -966,7 +966,8 @@ protected internal virtual void ChangeVisualState()
}
else
{
VisualStateManager.GoToState(this, VisualStateManager.CommonStates.Normal);
if(!VisualStateManager.GoToState(this, VisualStateManager.CommonStates.Normal))
VisualStateManager.ResetCommonStatesGroupSetters(this);
}
}

31 changes: 31 additions & 0 deletions Xamarin.Forms.Core/VisualStateManager.cs
Original file line number Diff line number Diff line change
@@ -17,6 +17,8 @@ public class CommonStates
public const string Selected = "Selected";
}

public const string CommonStatesGroupName = "CommonStates";

public static readonly BindableProperty VisualStateGroupsProperty =
BindableProperty.CreateAttached("VisualStateGroups", typeof(VisualStateGroupList), typeof(VisualElement),
defaultValue: null, propertyChanged: VisualStateGroupsPropertyChanged,
@@ -48,6 +50,35 @@ public static void SetVisualStateGroups(VisualElement visualElement, VisualState
visualElement.SetValue(VisualStateGroupsProperty, value);
}

// Use this as a fallback if the user did not define all the common states and got into an undesireable state
public static bool ResetCommonStatesGroupSetters(VisualElement visualElement)
{
if (!visualElement.HasVisualStateGroups())
{
return false;
}

var groups = (IList<VisualStateGroup>)visualElement.GetValue(VisualStateGroupsProperty);

foreach (VisualStateGroup group in groups)
{
if (group.Name == CommonStatesGroupName)
{
if (group.CurrentState != null)
{
foreach (Setter setter in group.CurrentState.Setters)
{
setter.UnApply(visualElement);
}
group.CurrentState = null;
return true;
}
}
}

return false;
}

public static bool GoToState(VisualElement visualElement, string name)
{
if (!visualElement.HasVisualStateGroups())