Change Grid Margin Using Visual State | UWP, WinUI3

Anderson Rodrigues Cavalcante 291 Reputation points
2024-06-12T19:09:25.5566667+00:00

I have a grid inside a Style. If this grid has a width > 640 I'd like to change its background for red. If the grid width <= 640 I'd like to change the background for yellow.

How can I do it only using xaml, without code behind?

Obs: It is the grid width, not window width (Adaptative.Trigger MinWindowWidth)

Universal Windows Platform (UWP)
XAML
XAML
A language based on Extensible Markup Language (XML) that enables developers to specify a hierarchy of objects with a set of properties and logic.
779 questions
{count} votes

Accepted answer
  1. Junjie Zhu - MSFT 16,151 Reputation points Microsoft Vendor
    2024-06-13T02:44:56.7733333+00:00

    Hi @Anderson Rodrigues Cavalcante ,

    Welcome to Microsoft Q&A!

    According to the document, VisualStateManager only applies to classes that derive from Control, In WinUI3, Window is not a Control, so VisualStateManager does not work with it.

    For UWP, you can use the code in Page.

        <Grid x:Name="grid" Background="Yellow">
            <VisualStateManager.VisualStateGroups>
                <VisualStateGroup x:Name="VisualStateGroup">
                    <VisualState x:Name="forTablet">
                        <VisualState.StateTriggers>
                            <AdaptiveTrigger MinWindowWidth="600"/>
                        </VisualState.StateTriggers>
                        <VisualState.Setters>
                            <Setter Target="grid.Background" Value="Red"/>
                        </VisualState.Setters>
                    </VisualState>
                </VisualStateGroup>
            </VisualStateManager.VisualStateGroups>
        </Grid>
    

    But for WinUI3, you need to add a page for the Gird.

    <?xml version="1.0" encoding="utf-8"?>
    <Window
         ...
         ... >
        <Page>
            <Grid x:Name="grid" Background="Yellow">
                <VisualStateManager.VisualStateGroups>
                    <VisualStateGroup x:Name="VisualStateGroup">
                        <VisualState x:Name="forTablet">
                            <VisualState.StateTriggers>
                                <AdaptiveTrigger MinWindowWidth="600"/>
                            </VisualState.StateTriggers>
                            <VisualState.Setters>
                                <Setter Target="grid.Background" Value="Red"/>
                            </VisualState.Setters>
                        </VisualState>
                    </VisualStateGroup>
                </VisualStateManager.VisualStateGroups>
            </Grid>
        </Page>
            
    </Window>
    

    Thank you.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful