首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >用RelativePanel构建自适应布局

用RelativePanel构建自适应布局
EN

Stack Overflow用户
提问于 2015-10-19 21:48:07
回答 2查看 993关注 0票数 0

你好,我希望当视力小于720的时候,比如手机,我希望网格2低于网格1。我试着通过它的面板和自适应触发器:

代码语言:javascript
复制
  <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>

但不起作用。谢谢

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-10-20 01:35:34

有两件事您需要修复:

1)使VisualState工作,将VisualStateManager放在根网格的第一个子网格下:

代码语言:javascript
复制
<Grid Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
        <VisualStateManager.VisualStateGroups>
......
        </VisualStateManager.VisualStateGroups>
......

</Grid>

2)您不需要添加列,只需将两个网格放在RelativePanel下:

代码语言:javascript
复制
<RelativePanel>
            <Grid x:Name="Grid1">
                ......
            </Grid>
            <Grid x:Name="Grid2">
                ......
            </Grid>
        </RelativePanel>

已完成的xaml代码:

代码语言:javascript
复制
<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
EN

Stack Overflow用户

发布于 2015-10-20 10:36:16

我找到了一个没有relativePanel的解决方案,如果有人关心的话:

代码语言:javascript
复制
<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>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/33224499

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档