WPF 窗口布局在运行时看起来与 VS 设计器不同
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19101525/
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
WPF window layout looks different from VS designer when running
提问by bitfrickler
I've recently been playing around with WPF a little bit. I'm a little confused as my program looks different when it's running from what I created in the designer.
我最近一直在玩 WPF。我有点困惑,因为我的程序在运行时看起来与我在设计器中创建的不同。
I'm certain that there is a valid reason for this but I can't wrap my head around why something so basic has to be so "mysterious".
我确信这样做是有正当理由的,但我无法理解为什么如此基本的东西必须如此“神秘”。
To be specific, I mean the bottom and right margin between the button and the inner border of the window.
具体来说,我的意思是按钮和窗口内边框之间的底部和右边距。
Designer:
设计师:
Running program:
运行程序:
Hope someone can help with this.
希望有人可以帮助解决这个问题。
XAML:
XAML:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Button Content="Button" HorizontalAlignment="Left" Margin="432,289,0,0"
VerticalAlignment="Top" Width="75" RenderTransformOrigin="-1.387,-0.75"/>
</Grid>
采纳答案by HichemSeeSharp
That's simply because :
那只是因为:
- There are d:Width and d:Height that affect design time and Height and Width that affect the run time. so verify that they are both the same.
- If you want to keep the margin from the bottom you have to specify it in XAML or click on the little margin holders from the bottom and the right.
- Unless you have Expression Blend, Don't rely on the VS drag and drop, instead, write your own XAML.
- 有影响设计时间的 d:Width 和 d:Height 以及影响运行时间的 Height 和 Width。因此请验证它们是否相同。
- 如果要保留底部的边距,则必须在 XAML 中指定它或单击底部和右侧的小边距固定器。
- 除非您有 Expression Blend,否则不要依赖 VS 拖放,而是编写您自己的 XAML。
Something like this would be very logical:
像这样的事情将是非常合乎逻辑的:
HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,10,10"
回答by Lightor
I had this issue as well and found a pretty easy solution for anyone else who runs into this.
我也遇到了这个问题,并为遇到此问题的其他人找到了一个非常简单的解决方案。
- Place a grid in the window, size as desired.
- Set window height and width to auto
- Set the window's
SizeToContentasWidthAndHeight
- 在窗口中放置一个网格,根据需要调整大小。
- 将窗口高度和宽度设置为自动
- 将窗口设置
SizeToContent为WidthAndHeight
Presto, the grid is a set size and the window will now size to it, no need to worry about the border or title bar.
Presto,网格是一个设定的大小,窗口现在将调整到它的大小,无需担心边框或标题栏。
回答by Itzik Gili
Seems like its the space of the border and the top bar. when changing the window style property of the window to none (WindowStyle="None"), it seems like the button is placed as the designer.
似乎是边框和顶部栏的空间。将窗口的窗口样式属性更改为无(WindowStyle =“无”)时,似乎按钮被放置为设计器。
VS considers that you wish the window size to include the borders and the title bar.
VS 认为您希望窗口大小包括边框和标题栏。


