【问题标题】:UWP grid to fill parent windowUWP 网格填充父窗口
【发布时间】:2016-11-05 01:54:51
【问题描述】:

我们正在开展一个学校项目,但遇到了死胡同。我们试图让grid 填满整个父窗口,但我们根本做不到。

这是设计师展示的内容以及我们希望它的外观:

这就是我们运行它时的样子:

这是我们的 xaml 代码:

<Grid x:Name="Grid" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="{Binding ActualWidth, RelativeSource = {RelativeSource Mode=TemplatedParent}}" Height="{Binding ActualHeight, RelativeSource ={RelativeSource Mode=TemplatedParent}}">
    <Grid.Background>
        <ImageBrush Stretch="UniformToFill" ImageSource="Assets/france_countryside.jpg" Opacity="0.4" />
    </Grid.Background>

    <!--Search section-->

    <RelativePanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Width="{Binding ElementName=Grid,Path=ActualWidth}">
        <TextBlock Text="Find available apartment" FontSize="24" Margin="30" RelativePanel.AlignHorizontalCenterWithPanel="True" />
        <AutoSuggestBox Name="AutoSuggestBox" 
                        PlaceholderText="Search" 
                        Width="300" 
                        RelativePanel.AlignHorizontalCenterWithPanel="True" 
                        Margin="0,100,0,0"
                        TextChanged="AutoSuggestBox_OnTextChanged"
                        Header="Destination:"/>
        <CalendarDatePicker Name="CheckInPicker" Header="Check in:" RelativePanel.Below="AutoSuggestBox" RelativePanel.AlignLeftWith="AutoSuggestBox" Margin="0,40,0,0" PlaceholderText="select a date" IsTodayHighlighted="False"/>
        <CalendarDatePicker Name="CheckOutPicker" Header="Check out:" RelativePanel.Below="AutoSuggestBox" RelativePanel.AlignRightWith="AutoSuggestBox" Margin="0,40,0,0"/>
        <ComboBox x:Name="numberOfGuestsBox"  Width="127" RelativePanel.Below="CheckInPicker" RelativePanel.AlignLeftWith="AutoSuggestBox" Margin="0,30,0,0" PlaceholderText="Choose" Header="Guests:" FontSize="15">
            <x:String>1</x:String>
            <x:String>2</x:String>
            <x:String>3</x:String>
            <x:String>4</x:String>
            <x:String>5</x:String>
            <x:String>6</x:String>
            <x:String>7</x:String>
            <x:String>8</x:String>
            <x:String>9</x:String>
            <x:String>10</x:String>
        </ComboBox>
        <ToggleSwitch Header="Smoking allowed?" Margin="0,30,0,0" RelativePanel.Below="CheckOutPicker" RelativePanel.AlignLeftWith="CheckOutPicker" OffContent="Eew - No!" OnContent="Ya man!"/>
        <Button x:Name="SearchButton" Content="Search available apartments" RelativePanel.Below="numberOfGuestsBox" RelativePanel.AlignHorizontalCenterWithPanel="True" Margin="0,30,0,30" Width="300" Height="50" Background="MediumSeaGreen" Foreground="AliceBlue" Click="SearchButton_Click"/>
    </RelativePanel>
</Grid>

我们该怎么做?
我们已经尝试过在我们看来一切都需要拉伸的东西。保证金并不是一个真正的选择,因为我们希望它能够重新调整大小。

似乎(对我们而言)网格正在适合相关面板并缩小到该大小。我们有点确定,如果我们让网格适合窗口的屏幕大小,则相关面板将被放置在中间。提前感谢您的帮助!


编辑:

我们将“视图”保存在可能导致问题的框架内。当我重新调整框架大小时,图像重新调整大小并且拆分视图移动到“中间”,但缩放不适用于拆分视图和图片。

这是拆分视图的代码:

    <!--Split View-->
    <SplitView Name="MySplitView" 
               Grid.Row="1" 
               DisplayMode="CompactOverlay" 
               OpenPaneLength="200" 
               CompactPaneLength="48" 
               HorizontalAlignment="Left">

        <!--SplitView Pane for icons-->
        <SplitView.Pane>
            <ListBox Name="IconsLIstBox" SelectionMode="Single" SelectionChanged="IconsLIstBox_OnSelectionChanged">
                <ListBoxItem Name="HomeListItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="24" Text="&#xE10F;"/>
                        <TextBlock Text="Home" Margin="20,0,0,0"/>
                    </StackPanel>
                </ListBoxItem>
                <ListBoxItem Name="LocationsListBoxItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="24" Text="&#xE1D1;"/>
                        <TextBlock Text="Locations" Margin="20,0,0,0"/>
                    </StackPanel>
                </ListBoxItem>
                <ListBoxItem Name="MostPopularListBoxItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="24" Text="&#xE24A;"/>
                        <TextBlock Text="Most Popular" Margin="20,0,0,0"/>
                    </StackPanel>
                </ListBoxItem>
                <ListBoxItem Name="MapListBoxItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="24" Text="&#xE128;"/>
                        <TextBlock Text="Map" Margin="20,0,0,0"/>
                    </StackPanel>
                </ListBoxItem>
                <ListBoxItem Name="ProfileListBoxItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="24" Text="&#xE170;"/>
                        <TextBlock Text="Profile" Margin="20,0,0,0"/>
                    </StackPanel>
                </ListBoxItem>
                <ListBoxItem Name="ContactListBoxItem">
                    <StackPanel Orientation="Horizontal">
                        <TextBlock FontFamily="Segoe MDL2 Assets" FontSize="24" Text="&#xE166;"/>
                        <TextBlock Text="Contact" Margin="20,0,0,0"/>
                    </StackPanel>
                </ListBoxItem>
            </ListBox>
        </SplitView.Pane>

        <!--SplitView Content-->
        <Frame x:Name="MyFrame" HorizontalAlignment="Left" Width="1043"/>
    </SplitView>

</Grid>

我们已经尝试将框架放在 splitview.content 中,但两者之间没有区别。

【问题讨论】:

  • 可以给我看看你的背景图片吗?
  • 不确定您的意思?网格中的图像如发布的图像中所示?

标签: c# xaml grid uwp


【解决方案1】:

您正在努力 :) 有时 XAML 很容易。

GridRelativePanel 这样的容器控件会自动缩放到其父元素的完整可用大小,而像 StackPanel 这样的其他容器控件只会增长到其子元素所需的最小大小。只有后一种类型需要HorizontalAlignment="Stretch"VerticalAlignment="Stretch" 才能填满屏幕。永远不要绑定 Width/Height 属性。

这应该足以全屏显示(如果您的网格直接在页面下方,而不是在 StackPanel 或类似控件中):

<Grid x:Name="Grid">
    <Grid.Background>
        <ImageBrush Stretch="UniformToFill" ImageSource="Assets/france_countryside.jpg" Opacity="0.4" />
    </Grid.Background>

    <!--Search section-->

    <RelativePanel>
        ...
    </RelativePanel>
</Grid>

编辑以回复问题中添加的拆分视图代码:

我注意到SplitViewFrame 都有HorizontAlignment="Left"。这就是说:“不要使用我的全屏,只使用您最低限度需要的任何尺寸并左对齐”。删除这些分配和Frame 的宽度。当您想要填充父控件时,请避免使用对齐方式(左/右/中心)或大小(宽度/高度)。

<!--Split View-->
<SplitView Name="MySplitView" 
           Grid.Row="1" 
           DisplayMode="CompactOverlay" 
           OpenPaneLength="200" 
           CompactPaneLength="48">
....

    <!--SplitView Content-->
    <Frame x:Name="MyFrame" />
</SplitView>

【讨论】:

  • 我们也试过这个。我想我已将错误本地化到另一个地方。正如您从第二张图片中看到的那样,我们有一个位于splitview 中的汉堡菜单,并且内容被加载到一个框架中(来自第一张图片的视图)。有没有办法让框架填满窗口?因为当我将框架调整为 800 时,图像和相关面板会相应地调整大小并移动!
  • 用代码检查我的编辑。它与除了导航之外的链接几乎相同。
  • 也更新了答案。尝试实现全屏行为时,切勿使用对齐/大小。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-24
  • 1970-01-01
  • 2015-06-10
  • 2016-02-08
  • 2011-01-09
  • 2013-07-19
相关资源
最近更新 更多