maui icon indicating copy to clipboard operation
maui copied to clipboard

Inheriting Visual States

Open kubaflo opened this issue 1 year ago • 4 comments
trafficstars

Description of Change

Visual states from parent types aren't being applied to derived types.

I have a button that when clicked switches between a Normal and a Custom state: VisualStateManager.GoToState(button, !isNormal ? "Custom" : "Normal");

Everything works as expected when both states are defined in the same VisualStateGroup:

<Style TargetType="Button">
    <Setter Property="VisualStateManager.VisualStateGroups">
        <VisualStateGroupList>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState Name="Normal">
                    <VisualState.Setters>
                        <Setter Property="TextColor" Value="White" />
                        <Setter Property="BackgroundColor" Value="Black" />
                    </VisualState.Setters>
                </VisualState>
                <VisualState Name="Custom">
                    <VisualState.Setters>
                        <Setter Property="TextColor" Value="Red" />
                        <Setter Property="BackgroundColor" Value="Green" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateGroupList>
    </Setter>
</Style>

The issue occurs when one of the visual states is defined in the parent Style. In that context the Custom style is ignored - the same happens when the visual state is defined in the global styles.

<Style TargetType="Button" x:Key="parentStyle">
    <Setter Property="VisualStateManager.VisualStateGroups">
        <VisualStateGroupList>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState Name="Custom">
                    <VisualState.Setters>
                        <Setter Property="TextColor" Value="Red" />
                        <Setter Property="BackgroundColor" Value="Green" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateGroupList>
    </Setter>
</Style>

<Style TargetType="Button" BasedOn="{StaticResource parentStyle}">
    <Setter Property="VisualStateManager.VisualStateGroups">
        <VisualStateGroupList>
            <VisualStateGroup x:Name="CommonStates">
                <VisualState Name="Normal">
                    <VisualState.Setters>
                        <Setter Property="TextColor" Value="White" />
                        <Setter Property="BackgroundColor" Value="Black" />
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateGroupList>
    </Setter>
</Style>

This PR is supposed to fix it by adding the visual state that has not been defined in the derived style from the parent style to the derived style

Before After

kubaflo avatar Feb 09 '24 14:02 kubaflo

Hey there @kubaflo! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

ghost avatar Feb 09 '24 14:02 ghost

@kubaflo Your work seems to be, to some degree, related to #19812. Any feedback would be awesome.

MartyIX avatar Feb 10 '24 09:02 MartyIX

/azp-run

jsuarezruiz avatar Apr 19 '24 10:04 jsuarezruiz

/azp run

jsuarezruiz avatar May 13 '24 10:05 jsuarezruiz

Azure Pipelines successfully started running 3 pipeline(s).

azure-pipelines[bot] avatar May 13 '24 10:05 azure-pipelines[bot]