简体   繁体   English

如何为不同的屏幕尺寸和不同的窗口尺寸固定Gridview高度?

[英]How to fix the Gridview Height for different screen sizes and different window sizes?

Just a simple question really that I haven't been able to find a satisfying answer to. 只是一个简单的问题,我一直无法找到令人满意的答案。 Working on a UWP app and in some parts I have a GridView within a Grid. 在UWP应用程序上工作,在某些部分中,我在Grid中有一个GridView。 Of course in order to enable scrolling in the app I had to give the GridView a height (let's say 650) this height might work on small screens but then on larger ones half of the app is unused!... even just when resizing the app window height it seems that the GridView would work in some sizes but not others... how do I fix this issue or at least get around it ? 当然,为了在应用程序中滚动,我不得不给GridView一个高度(比如650),这个高度可能在小屏幕上起作用,但是在较大的屏幕上,一半的应用程序都没有使用!应用程式视窗高度,看来GridView可以在某些尺寸上运作,但在其他尺寸下无法运作...我该如何解决这个问题,或者至少可以解决这个问题? I want the height of the Gridview linked to the height of the app window. 我想将Gridview的高度链接到应用程序窗口的高度。 any ideas ? 有任何想法吗 ?

When you have a GridView inside a Grid you can always just give its RowDefinition and ColumnDefinition a value of "*" 当您在Grid内拥有GridView时,始终可以将其RowDefinitionColumnDefinition的值设置为"*"

Then it will always take up the undistributed space on the window. 然后,它将始终占用窗口上未分配的空间。

A small example: 一个小例子:

<Grid>
    <Grid.RowDefinitions>
        <RowDefinition Height="150"/> 
        <RowDefinition Height="*" 
                       MinHeight="50" 
                       MaxHeight="500"/>
        <RowDefinition Height="150"/>
    </Grid.RowDefinitions>

    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="150"/>
        <ColumnDefinition Width="*" 
                          MinWidth="100" 
                          MaxWidth="1000"/>
        <ColumnDefinition Width="150"/>
    </Grid.ColumnDefinitions>

    <GridView Grid.Row="1"
              Grid.Column="1">

    </GridView>
</Grid>

Note that I also added values for the maximum and minimum height/width. 请注意,我还添加了最大和最小高度/宽度的值。 With that you can further limit the size of the item in the grid. 这样,您可以进一步限制网格中项目的大小。

The only thing you should keep in mind is that when you use the "*" value then it will take up all the rest of the space (within its limits if max/min is set). 您唯一需要记住的是,当您使用"*"值时,它将占用所有剩余空间(如果设置了max / min,则在其限制之内)。 But when you have two definitions with that value like this: 但是,当您有两个具有该值的定义时,如下所示:

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

Then both columns/rows will take up the same amount of space. 然后,两列/行将占用相同的空间量。 Say we have a window width of 800 then both columns will share the middle 500. With both of them taking up 250 each. 假设我们的窗口宽度为800,那么两列将共享中间的500。两列各占250。

You can also write it like this to vary it: 您也可以像这样编写它来改变它:

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

Which will assign the second column twice the screen width of the first column. 这将为第二列分配两倍于第一列的屏幕宽度。 So let's say we have a screen width of 900. Then the first column will be 300 wide and the second column will be 600 wide. 假设我们的屏幕宽度为900。第一列的宽度为300,第二列的宽度为600。

for making app for different screen sizes then keep one thing in mind avoid static row and column height and width . 为了使应用程序适应不同的屏幕尺寸,请注意一件事,避免静态行和列的高度和宽度。

your grid somthing like this. 您的网格像这样。

<Grid>
<Grid.RowDefinitions>
        <RowDefinition Height="*"/>
        <RowDefinition Height="*"/>
</Grid.RowDefinitions>

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

and also dont forget to set verticaloption and Horizontaloption as per your requirement. 并且不要忘记根据您的要求设置verticaloptionHorizontaloption

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM