Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: VisualStateManagerExtensions.OverrideStates -> States #344

Merged
merged 1 commit into from
Oct 5, 2022
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
10 changes: 5 additions & 5 deletions doc/helpers/VisualStateManager-extensions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
Provides a way of manipulating the visual states of `Control` with attached property.

## Remarks
`VisualStateManager.GoToState` is typically used with `Control` where you would set `<VisualStateManager.VisualStateGroups>` on the root element of the ControlTemplate. Because this class is implemented using the same method, it means that if you are setting `OverrideStatesProperty` on an element, the `VisualStateManager.VisualStateGroups` should not be set on the very same element, but its first child:
`VisualStateManager.GoToState` is typically used with `Control` where you would set `<VisualStateManager.VisualStateGroups>` on the root element of the ControlTemplate. Because this class is implemented using the same method, it means that if you are setting `StatesProperty` on an element, the `VisualStateManager.VisualStateGroups` should not be set on the very same element, but its first child:
```xml
<Page utu:VisualStateManagerExtensions.OverrideStates="{Binding OnboardingState}">
<Page utu:VisualStateManagerExtensions.States="{Binding OnboardingState}">
<!-- Wrong -->
<VisualStateManager.VisualStateGroups>...

Expand All @@ -17,9 +17,9 @@ This "first child" is more common known as the template root within the context
## Properties
Property|Type|Description
-|-|-
OverrideStates|string|Sets the visual states of the control.\*
States|string|Sets the visual states of the control.\*

OverrideStates\*: The accepted value can be a space, comma or semi-colon separated list of visual state names. eg:
States\*: The accepted value can be a space, comma or semi-colon separated list of visual state names. eg:
- "LoggedIn": just a single state
- "LoggedIn, OnMeteredNetwork": two concurrent states from different visual state groups
- "Pressed, Selected, PressedSelected": with a combined state
Expand All @@ -29,7 +29,7 @@ OverrideStates\*: The accepted value can be a space, comma or semi-colon separat
```xml
<Page ...
xmlns:utu="using:Uno.Toolkit.UI"
utu:VisualStateManagerExtensions.OverrideStates="{Binding PageState}">
utu:VisualStateManagerExtensions.States="{Binding PageState}">
<Grid ColumnDefinitions="Auto,*" RowDefinitions="*,*,*">

<VisualStateManager.VisualStateGroups>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<sample:SamplePageLayout.DesignAgnosticTemplate>
<DataTemplate>
<UserControl DataContext="{Binding Data}"
utu:VisualStateManagerExtensions.OverrideStates="{Binding PageState}"
utu:VisualStateManagerExtensions.States="{Binding PageState}"
Margin="0,16,0,0">
<Grid>
<Grid.ColumnDefinitions>
Expand Down
16 changes: 8 additions & 8 deletions src/Uno.Toolkit.UI/Behaviors/VisualStateManagerExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,34 @@ namespace Uno.Toolkit.UI;
/// <remarks>
/// <seealso cref="VisualStateManager.GoToState"/> is typically used with <see cref="Control"/>
/// where you would set <see cref="VisualStateManager.VisualStateGroupsProperty"/> on the root element of the ControlTemplate.
/// Because this class directly calls that method, it means that if you are setting <see cref="OverrideStatesProperty"/> on an element,
/// Because this class directly calls that method, it means that if you are setting <see cref="StatesProperty"/> on an element,
/// the <see cref="VisualStateManager.VisualStateGroupsProperty"/> should not be set on the very same element, but its first child.
/// </remarks>
public static class VisualStateManagerExtensions
{
private static readonly ILogger Logger = typeof(VisualStateManagerExtensions).Log();

#region DependencyProperty: OverrideStates
#region DependencyProperty: States

/// <summary>
/// Identifies the OverrideStates dependency property.
/// Identifies the States dependency property.
/// </summary>
public static DependencyProperty OverrideStatesProperty { get; } = DependencyProperty.RegisterAttached(
"OverrideStates",
public static DependencyProperty StatesProperty { get; } = DependencyProperty.RegisterAttached(
"States",
typeof(string),
typeof(VisualStateManagerExtensions),
new PropertyMetadata(default(string), OnOverrideStatesChanged));
new PropertyMetadata(default(string), OnStatesChanged));

/// <summary>
/// Sets the visual states of the control.
/// </summary>
/// <param name="obj"></param>
/// <param name="value">A space, comma or semi-colon separated list of visual state names</param>
public static void SetOverrideStates(Control obj, string value) => obj.SetValue(OverrideStatesProperty, value);
public static void SetStates(Control obj, string value) => obj.SetValue(StatesProperty, value);

#endregion

private static void OnOverrideStatesChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
private static void OnStatesChanged(DependencyObject sender, DependencyPropertyChangedEventArgs e)
{
if (sender is Control control && e.NewValue is string { Length: >0 } value)
{
Expand Down