Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Visual State Manager in Windows 10 UWP not applying initial state on page load

I have a page with a relative panel to re-organize based on width. However, it doesn't seem to apply any state at load unless the width is > 720px. If I resize the page after it's loaded both states work.

A workaround would be to check the window size on page loaded and manually choose the state, but I believe this should be handled automatically? I have other pages that work, I'm not sure what I'm doing different. Here is a simplified version of my code, I have it set red/blue backgrounds so I can tell if/which state is being applied

<Page.Resources>
    <converters:HighlightConverter x:Key="HighlightConverter"/>
</Page.Resources>

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="*"/>
    </Grid.RowDefinitions>

    <gui:MainAppBar x:Name="mainAppBar" Grid.Row="0"/>

    <ScrollViewer Grid.Row="1">
        <RelativePanel>

            <StackPanel x:Name="ZonesContainer" Margin="12,12,0,0">
                <TextBlock Text="Zones"/>
                <ItemsControl x:Name="ZonesPanel">
                    <ItemsControl.ItemContainerStyle>
                        <Style TargetType="ContentPresenter">
                            <Setter Property="Margin" Value="6"/>
                        </Style>
                    </ItemsControl.ItemContainerStyle>
                    <ItemsControl.ItemsPanel>
                        <ItemsPanelTemplate>
                            <ItemsWrapGrid x:Name="ZonesWrapGrid" Orientation="Vertical"/>
                        </ItemsPanelTemplate>
                    </ItemsControl.ItemsPanel>
                    <ItemsControl.ItemTemplate>
                        <DataTemplate>
                            <StackPanel x:Name="Panel" Orientation="Horizontal">
                            </StackPanel>
                        </DataTemplate>
                    </ItemsControl.ItemTemplate>
                </ItemsControl>
            </StackPanel>

            <StackPanel x:Name="SourcesContainer" RelativePanel.RightOf="ZonesContainer" Margin="12,12,0,0">
                <GridView x:Name="SourcesPanel" Header="Sources">
                </GridView>
            </StackPanel>

            <StackPanel x:Name="NetworkServicesContainer" RelativePanel.Below="SourcesContainer" RelativePanel.AlignLeftWith="SourcesContainer" Margin="12,12,0,0">
                <GridView x:Name="NetworkServicesPanel" Header="Network">
                </GridView>
            </StackPanel>

        </RelativePanel>
    </ScrollViewer>

    <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="WindowStates">
            <VisualState x:Name="WideState">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="720" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="ZonesContainer.Background" Value="Blue"/>
                </VisualState.Setters>
            </VisualState>
            <VisualState x:Name="NarrowState">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="0" />
                </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="ZonesContainer.Background" Value="Red"/>
                    <Setter Target="SourcesContainer.(RelativePanel.Below)" Value="ZonesContainer" />
                    <Setter Target="SourcesContainer.(RelativePanel.AlignLeftWith)" Value="ZonesContainer" />
                    <Setter Target="NetworkServicesContainer.(RelativePanel.Below)" Value="SourcesContainer" />
                    <Setter Target="ZonesWrapGrid.Orientation" Value="Horizontal" />
                </VisualState.Setters>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

</Grid>

  • Update

I've updated the code to show the missing ZonesWrapGrid, it does seem to be related. The visual states do work on it when I resize the page it will switch the ZonesWrapGrid orientation, just no state set on load.

However, if I remove the ZonesWrapGrid change from the visual state manager the narrow/wide states do correctly apply on load, but of course I lose the orientation change I want.

like image 309
Jason Avatar asked Oct 12 '15 01:10

Jason


1 Answers

Can you try just replacing the whole ItemsPanelTemplate? Create both in resources:

<ItemsPanelTemplate x:Key="VerticalWrapGrid">
    <ItemsWrapGrid Orientation="Vertical"/>
</ItemsPanelTemplate>
<ItemsPanelTemplate x:Key="HorizontalWrapGrid">
    <ItemsWrapGrid Orientation="Horizontal"/>
</ItemsPanelTemplate>

And then swap when needed:

<VisualState x:Name="WideState">
    <VisualState.StateTriggers>
        <AdaptiveTrigger MinWindowWidth="720" />
    </VisualState.StateTriggers>
    <VisualState.Setters>
        <Setter Target="ZonesPanel.ItemsPanel" Value="{StaticResource HorizontalWrapGrid}" />
    </VisualState.Setters>
</VisualState>
<VisualState x:Name="NarrowState">
    <VisualState.StateTriggers>
        <AdaptiveTrigger MinWindowWidth="0" />
    </VisualState.StateTriggers>
    <VisualState.Setters>
        <Setter Target="ZonesPanel.ItemsPanel" Value="{StaticResource VerticalWrapGrid}" />
    </VisualState.Setters>
</VisualState>
like image 135
Igor Ralic Avatar answered Nov 03 '22 12:11

Igor Ralic