wpf 调整内容大小以适应屏幕分辨率
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1114385/
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
Sizing the content to fit into screen resolution
提问by Chen Kinnrot
hello i have a window(wpf) with labels and text boxes, i want him to fit to the screen resolution as much as possible, how do i do it
你好,我有一个带有标签和文本框的窗口(wpf),我希望他尽可能适应屏幕分辨率,我该怎么做
回答by Orporick
Viewbox is quite useful if you need the content of your window to scale proportionally when you resize the window (for example maximize it). In this minimalistic page
如果您需要在调整窗口大小时按比例缩放窗口内容(例如最大化它),则 Viewbox 非常有用。在这个简约的页面中
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300">
<Viewbox>
<StackPanel>
<TextBlock FontSize="14">Example</TextBlock>
<Border Background="Aqua" Width="100" Height="100"></Border>
</StackPanel>
</Viewbox>
</Window>
you have a TextBlock and a colored Border stacked vertically; if you start this xaml the window will have a size of 300x300, the font of the TextBlock will be 14 in size and the colored border will be 100x100. If you rescale the window you will see both the TextBlock and the Border scale accordingly (so they'll be no more of the size you've specified in the xaml), keeping relative proportions. Viewbox is really useful, in this respect, if you need a window whose internal components layout look always the same independently from the final resolution it will be displayed (what does matter is aspect-ratio, thought). This obviously work with any contents you'll put inside the Viewbox (we had an application with videos and 3D views for example). Note that in Visual Studio 2008 you'll not be able to see the content of the Viewbox in the Designer.
你有一个 TextBlock 和一个垂直堆叠的彩色边框;如果您启动此 xaml,窗口的大小将为 300x300,TextBlock 的字体大小为 14,彩色边框为 100x100。如果您重新缩放窗口,您将看到相应的 TextBlock 和 Border 比例(因此它们将不再是您在 xaml 中指定的大小),并保持相对比例。Viewbox 非常有用,在这方面,如果您需要一个窗口,其内部组件布局看起来总是与最终分辨率无关,它将被显示(重要的是纵横比,思想)。这显然适用于您将放入 Viewbox 的任何内容(例如,我们有一个包含视频和 3D 视图的应用程序)。请注意,在 Visual Studio 2008 中,您
Hope this help.
希望这有帮助。
回答by Botz3000
If you want to scale really everything including font sizes, you could probably apply a scale transform to your content, and bind it's X and Y values to the window's width and height. You would then also need a value converter to convert those to the appropriate scale.
如果您真的想缩放包括字体大小在内的所有内容,您可以对内容应用缩放变换,并将其 X 和 Y 值绑定到窗口的宽度和高度。然后,您还需要一个值转换器来将它们转换为适当的比例。
回答by Nir
If you want to scale everything to the size of the window just put everything inside a Viewbox control.
如果您想将所有内容缩放到窗口大小,只需将所有内容放在 Viewbox 控件中。
回答by Daniel Earwicker
Do you mean you want the window to fill the entire screen? The simplest way to do that (without causing further headaches) is to maximise the window.
你的意思是你想让窗口填满整个屏幕?最简单的方法(不会引起进一步的头痛)是最大化窗口。
w.WindowState = WindowState.Maximized;
EDIT:
编辑:
A scalable window layout requires you to avoid using the XAML editor in Visual Studio! Actually you can do it in the editor, but it is very hard.
可缩放的窗口布局要求您避免在 Visual Studio 中使用 XAML 编辑器!其实你可以在编辑器里做,但是很难。
Much easier to write the XAML by hand:
手动编写 XAML 更容易:
<Grid>
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Label Grid.Column="0" Grid.Row="0">First Name</Label>
<TextBox Grid.Column="1" Grid.Row="0" Name="firstName">Fred</TextBox>
<Label Grid.Column="0" Grid.Row="1">First Name</Label>
<TextBox Grid.Column="1" Grid.Row="1" Name="lastName">Smith</TextBox>
</Grid>
This will size to fit the window, though may look strange, as the rows and columns will by default get half the space each. You can override this so they have a height determined by their contents instead:
这将调整大小以适应窗口,尽管可能看起来很奇怪,因为默认情况下行和列将获得一半的空间。您可以覆盖它,以便它们的高度由它们的内容决定:
<RowDefinition Height="Auto"/>
It can also help to put margins on some controls, to space them out:
它还可以帮助在某些控件上放置边距,以将它们隔开:
<TextBox Grid.Column="1" Grid.Row="1" Margin="6" Name="lastName">Smith</TextBox>
回答by Sachini Witharana
Add WindowState="Maximized"to the Window
将WindowState="Maximized"添加到窗口
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="Window1" Height="300" Width="300" WindowState="Maximized" >
</Window>