Building adaptive layout with RelativePanel

旧街凉风 提交于 2020-01-17 05:37:06

问题


hello I want that when the vision is less than 720 such as a phone, I would like the grid 2 went below the grid 1. I tried through its panel and adaptive trigger like this:

  <Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">

    <RelativePanel >
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="VisualStateGroup">
                <VisualState x:Name="NarrowView">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="0" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="Grid2.(RelativePanel.Below)" Value="Grid1"/>
                        <Setter Target="Grid1.(RelativePanel.Above)" Value="Grid2"/>
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="WideView">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="720" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="Grid2.(RelativePanel.RightOf)" Value="Grid1"/>
                        <Setter Target="Grid1.(RelativePanel.LeftOf)" Value="Grid2"/>
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>

        <Grid >
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="*"></ColumnDefinition>
                <ColumnDefinition Width="*"></ColumnDefinition>
            </Grid.ColumnDefinitions>

            <Grid Grid.Column="0" x:Name="Grid1" >
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
            </Grid.RowDefinitions>
                <TextBox Grid.Row="0" FontSize="20" PlaceholderText="NOME" Style="{StaticResource ResourceKey=TextBoxStyle}"/>
            </Grid>
            <Grid Grid.Column="1" x:Name="Grid2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" FontSize="17" Text="Note" Foreground="#222222" Margin="20,15" ></TextBlock>
                <TextBox Grid.Row="2" MaxLength="0" FontSize="17" Height="500" PlaceholderText="AGGIUNGI NOTA" Style="{StaticResource ResourceKey=TextBoxStyle}"></TextBox>
            </Grid>
        </Grid>
    </RelativePanel>
</Grid>

But it does not work. Thank you


回答1:


There are two things you need to fix:

1) To make VisualState works, place the VisualStateManager under the first child of root Grid:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <VisualStateManager.VisualStateGroups>
......
        </VisualStateManager.VisualStateGroups>
......

</Grid>

2) You don't need to add Columns, just place your two Grids under RelativePanel:

<RelativePanel>
            <Grid x:Name="Grid1">
                ......
            </Grid>
            <Grid x:Name="Grid2">
                ......
            </Grid>
        </RelativePanel>

The completed xaml code:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="VisualStateGroup">
                <VisualState x:Name="NarrowView">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="0" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="Grid2.(RelativePanel.Below)" Value="Grid1"/>
                    </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="WideView">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="720" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="Grid2.(RelativePanel.RightOf)" Value="Grid1"/>
                    </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <RelativePanel>
            <Grid x:Name="Grid1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBox Grid.Row="0" FontSize="20" PlaceholderText="NOME" />
            </Grid>
            <Grid x:Name="Grid2">
                <Grid.RowDefinitions>
                    <RowDefinition Height="Auto"/>
                    <RowDefinition Height="Auto"/>
                </Grid.RowDefinitions>
                <TextBlock Grid.Row="0" FontSize="17" Text="Note" Foreground="#222222" Margin="20,15" ></TextBlock>
                <TextBox Grid.Row="2" MaxLength="0" FontSize="17" Height="500" PlaceholderText="AGGIUNGI NOTA" ></TextBox>
            </Grid>
        </RelativePanel>
    </Grid>



回答2:


i found a solution without relativePanel, if anyone care:

<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <VisualStateManager.VisualStateGroups>
            <VisualStateGroup x:Name="VisualStateGroup">
                <VisualState x:Name="NarrowView">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="0" />
                    </VisualState.StateTriggers>
                <VisualState.Setters>
                    <Setter Target="text.(Grid.ColumnSpan)" Value="2" />
                    <Setter Target="text1.(Grid.ColumnSpan)" Value="2" />
                    <Setter Target="text2.(Grid.ColumnSpan)" Value="2" />
                    <Setter Target="text1.(Grid.Row)" Value="1" />
                    <Setter Target="text1.(Grid.Column)" Value="0" />
                    <Setter Target="text2.(Grid.Row)" Value="2" />
                    <Setter Target="text2.(Grid.Column)" Value="0" />
                </VisualState.Setters>
                </VisualState>
                <VisualState x:Name="WideView">
                    <VisualState.StateTriggers>
                        <AdaptiveTrigger MinWindowWidth="860" />
                    </VisualState.StateTriggers>
                    <VisualState.Setters>
                        <Setter Target="text.(Grid.ColumnSpan)" Value="1" />
                        <Setter Target="text1.(Grid.Row)" Value="0" />
                        <Setter Target="text1.(Grid.Column)" Value="1" />
                        <Setter Target="text2.(Grid.Row)" Value="1" />
                        <Setter Target="text2.(Grid.Column)" Value="1" />
                </VisualState.Setters>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="*"/>
            <ColumnDefinition Width="*"/>
        </Grid.ColumnDefinitions>
        <Grid.RowDefinitions>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
            <RowDefinition Height="Auto"/>
        </Grid.RowDefinitions>
    <TextBox x:Name="text" PlaceholderText="NOME" />
    <TextBlock x:Name="text1" Grid.Row="0" Grid.Column="1" Text="Note"></TextBlock>
    <TextBox x:Name="text2" Grid.Row="1" Grid.Column="1" PlaceholderText="AGGIUNGI NOTA" ></TextBox>
</Grid>


来源:https://stackoverflow.com/questions/33224499/building-adaptive-layout-with-relativepanel

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!