2008-11-14 38 views
32

我对WPF相当陌生,而且遇到了一个似乎有点棘手难题的问题。基本上我想要一个可扩展的4x4网格,但保持一个方形(或任何其他任意)的高宽比。这实际上看起来相当棘手,这令我感到惊讶,因为我会想象它是一个相当普遍的要求。如何保持WPF中可伸缩,可滚动内容的纵横比?

我开始与电网的定义是这样的:

<Grid> 
    <Grid.RowDefinitions> 
    <Grid.RowDefinition Height="*"/> 
    <Grid.RowDefinition Height="*"/> 
    <Grid.RowDefinition Height="*"/> 
    <Grid.RowDefinition Height="*"/> 
    </Grid.RowDefinitions> 
    <Grid.ColumnDefinitions> 
    <Grid.ColumnDefinition Width="*"/> 
    <Grid.ColumnDefinition Width="*"/> 
    <Grid.ColumnDefinition Width="*"/> 
    <Grid.ColumnDefinition Width="*"/> 
    </Grid.ColumnDefinition> 
    ... 
</Grid> 

现在,如果你设置伸展,它可以填补窗口或任何容器,你把它的行和列是统一的,但纵横。比例不固定。

然后我试着把它放在StackPanel中以使用可用空间。没有帮助。当我记住Viewboxes时,最让我感觉到的是什么。

<StackPanel Orientation="Horizontal"> 
    <Viewbox> 
    <Grid Height="1000" Width="1000"> <!-- this locks aspect ratio --> 
     <Grid.RowDefinitions> 
     <Grid.RowDefinition Height="*"/> 
     <Grid.RowDefinition Height="*"/> 
     <Grid.RowDefinition Height="*"/> 
     <Grid.RowDefinition Height="*"/> 
     </Grid.RowDefinitions> 
     <Grid.ColumnDefinitions> 
     <Grid.ColumnDefinition Width="*"/> 
     <Grid.ColumnDefinition Width="*"/> 
     <Grid.ColumnDefinition Width="*"/> 
     <Grid.ColumnDefinition Width="*"/> 
     </Grid.ColumnDefinition> 
     ... 
    </Grid> 
    </viewbox> 
    <Label HorizontalAlignment="Stretch">Extra Space</Label> 
</StackPanel> 

现在我的内容缩放并保持宽高比。问题是,如果窗口不够宽,我的一些网格不在屏幕上。如果是这种情况,我希望能够滚动到它。同样,我可能需要最小尺寸,这也可能导致垂直滚动。

现在我已经尝试将我的StackPanel和网格(单独)放入适当的ScrollViewer容器中,但随后内容不再缩放以适应窗口。它达到全尺寸,这是不好的。

那么我该如何去做这件事?我吠叫错了树吗?有没有更好/更简单的方法来做到这一点?

回答

-2

把0.25 *,而不是*为每列和RowWidth

0

窗口给你你后的行为上会设置SizeToContent =“WidthAndHeight”?

5

在这种情况下,最好的办法是UniformGrid。这只有在你需要NxN网格时才有用。