C# 如何在 WPF 中保持可缩放、可滚动内容的纵横比?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/288954/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-03 21:47:14  来源:igfitidea点击:

How do I keep aspect ratio on scalable, scrollable content in WPF?

c#.netwpfxaml

提问by cletus

I'm fairly new to WPF and I've come across a problem that seems a bit tricky to solve. Basically I want a 4x4 grid thats scalable but keeps a square (or any other arbitrary) aspect ratio. This actually seems quite tricky to do, which surprises me because I would imagine its a reasonably common requirement.

我对 WPF 还很陌生,我遇到了一个似乎有点难以解决的问题。基本上我想要一个可扩展的 4x4 网格,但保持方形(或任何其他任意)纵横比。这实际上看起来很棘手,这让我感到惊讶,因为我认为这是一个相当普遍的要求。

I start with a Grid definition like this:

我从这样的 Grid 定义开始:

<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>

Now if you set that to stretch, it can fill the Window or whatever container you put it in. The rows and column are uniform but the aspect ratio isn't fixed.

现在,如果您将其设置为拉伸,它可以填充窗口或您放入的任何容器。行和列是统一的,但纵横比不固定。

Then I tried putting it in a StackPanel to use the available space. Didn't help. What did get me most of the way there was when I remembered Viewboxes.

然后我尝试将它放在 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>

Now my content scales and keeps aspect ratio. The problem is that if the window isn't wide enough some of my grid is off the screen. I'd like to be able to scroll to it if that were the case. Likewise, I might need a minimum size, which might lead to vertical scrolling too.

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

Now I've tried putting my StackPanel and Grid (separately) in an appropriate ScrollViewer container but then the content no longer scales to fit the window. It goes to full size, which is no good.

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

So how do I go about doing this? Am I barking up the wrong tree? Is there a better/easier way of doing this?

那么我该怎么做呢?我是不是叫错了树?有没有更好/更简单的方法来做到这一点?

采纳答案by Pop Catalin

You need to put the content (the grid) inside a ViewBoxand set the Viewbox.Stretch Propertyto Stretch.Uniform

您需要将内容(网格)放在ViewBox 中,并将Viewbox.Stretch 属性设置为Stretch.Uniform

The Viewbox control is used to stretch or scale a child element and lets you control the way the child is stretched. Check the exampleshere.

Viewbox 控件用于拉伸或缩放子元素,并让您控制子元素的拉伸方式。检查这里的例子

alt text
(source: microsoft.com)

替代文字
(来源:microsoft.com

回答by Jobi Joy

put 0.25* instead of * for each Column and RowWidth

为每个 Column 和 RowWidth 放置 0.25* 而不是 *

回答by Donnelle

You need to set SizeToContent="WidthAndHeight"on the window to give you the behaviour you're after.

您需要SizeToContent="WidthAndHeight"在窗口上进行设置以提供您所追求的行为。

回答by user7116

In this instance, your best bet is a UniformGrid. This is only useful if you want NxN grids.

在这种情况下,最好的选择是UniformGrid。这仅在您需要 NxN 网格时才有用。