wpf 如何相对缩放用户控件的大小?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1417004/
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
How to relative scale size of User Control?
提问by Peter Stegnar
How to relative scale size of User Control like image (in image editor).
如何相对缩放用户控件的大小,如图像(在图像编辑器中)。
Example (100%):
示例 (100%):
(source: stegnar.com)
(来源:stegnar.com)
Scaled down UC (70%):
缩小 UC (70%):
(source: stegnar.com)
(来源:stegnar.com)
Well I achieve this in picture editor, but I would like in WPF. :) I need this to adjust my application to different screen resolution, while nothing hiding (no scrollbars).
好吧,我在图片编辑器中实现了这一点,但我想在 WPF 中实现。:) 我需要这个来将我的应用程序调整到不同的屏幕分辨率,同时没有隐藏(没有滚动条)。
回答by Milan Nankov
You could try the ViewBox control that scales up/down its content so that it fills the available space.
您可以尝试放大/缩小其内容以填充可用空间的 ViewBox 控件。
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication1"
Title="Window1">
<Grid>
<Viewbox StretchDirection="Both" Stretch="Uniform">
<local:UserControl1 Height="600" Width="600"/>
</Viewbox>
</Grid>
回答by eon
you can place the whole container into a ViewBox
您可以将整个容器放入 ViewBox
<Viewbox StretchDirection="Both" Stretch="Uniform">
<Grid>...</Grid>
</Viewbox>
you don't have to place each single textblock in it!
您不必将每个文本块都放在其中!
回答by Trainee4Life
Use of Viewbox (as said by Milan Nankov) is a great idea. One thing that I must warn you is that it also zooms in or out other visual aspects as well.
使用 Viewbox(如 Milan Nankov 所说)是一个好主意。我必须警告您的一件事是,它还可以放大或缩小其他视觉方面。
For example, a Textbox with dimension 200 X 1000 is very different from a Textbox with dimension 20 X 100 zoomed in 10x.
例如,尺寸为 200 X 1000 的文本框与放大 10 倍的尺寸为 20 X 100 的文本框非常不同。
WPF provides many layouting options which can change dimension of the controls according to the size of the container. But it doesn't change the size of the text. Viewbox overcomes this issue, but it introduces another issue. Check the image below which shows the same textbox in a viewbox before and after zooming.
WPF 提供了许多布局选项,可以根据容器的大小更改控件的尺寸。但它不会改变文本的大小。Viewbox 克服了这个问题,但它引入了另一个问题。检查下面的图像,该图像在缩放前后在视图框中显示了相同的文本框。
One trick which could be used is to place every textblockin a viewbox. But I guess that would be an overkill, and I seriously don't having any backing for this trick. Please do check for yourself and reply whether it's practical or not.
可以使用的一个技巧是将每个文本块放在一个视图框中。但我想这有点矫枉过正,我真的没有任何支持这个技巧。请自行检查并回复它是否实用。
Another trick could be to bind the control's height to the font size. We would be needing a converter in that case. Please refer to this reply.. Resize font in TextBox in Grid
另一个技巧可能是将控件的高度绑定到字体大小。在这种情况下,我们将需要一个转换器。请参考这个回复.. Resize font in TextBox in Grid