C# 对齐 WPF 视图框中的内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/342116/
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 23:53:12  来源:igfitidea点击:

Aligning content in a WPF Viewbox

c#wpfxamlviewbox

提问by Jeff B

I have a Viewbox with Stretch=Uniformin order to not distort the content. However, when the frame window is wider or taller than the content, the Viewbox content is always centered.

Stretch=Uniform为了不扭曲内容,我有一个 Viewbox 。但是,当框架窗口比内容宽或高时,Viewbox 内容始终居中。

I cannot seem to find any content alignment options on the Viewbox. Is there a way to do this?

我似乎在 Viewbox 上找不到任何内容对齐选项。有没有办法做到这一点?

采纳答案by John Z

Try VerticalAlignment="Top"and HorizontalAlignment="Left"on your viewbox. It will cause it to be anchored to the top and left side.

尝试VerticalAlignment="Top"HorizontalAlignment="Left"在您的视窗上。这将使其锚定到顶部和左侧。

<Grid>
    <Viewbox VerticalAlignment="Top" HorizontalAlignment="Left">
    ...
    </Viewbox>
</Grid>

If you want it to completely fill (but keep it uniform) you can use Stretch="UniformToFill"

如果您希望它完全填充(但保持均匀),您可以使用 Stretch="UniformToFill"

回答by a_hardin

According to MSDNthe Viewbox is used to stretch the child elements. Since the child elements would be stretched, you would have to set the content alignment of the children.

根据MSDN,Viewbox 用于拉伸子元素。由于子元素会被拉伸,您必须设置子元素的内容对齐方式。

You may want to look at this for more information on the Viewbox: How do I keep aspect ratio on scalable, scrollable content in WPF?

您可能想查看有关 Viewbox 的更多信息:如何在 WPF 中保持可缩放、可滚动内容的纵横比?