我在 Blend/VS2012 IDE
和 Debugging
中遇到不同外观的问题。
在IDE
中,Rectangle
是居中的,但是在编译或者调试的时候,边距的大小是不一样的。在我看来,出现这种情况是因为窗口边框的大小不同。我真的很想解决这个问题。有什么建议吗?
谢谢。
XAML
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="202" Width="194">
<Grid HorizontalAlignment="Left" Height="171" VerticalAlignment="Top" Width="186">
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="151" Margin="10,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="166"/>
</Grid>
编辑的 XAML
代码:
<Window x:Class="WpfApplication2.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="202" Width="194">
<Grid HorizontalAlignment="Left" Height="171" VerticalAlignment="Top" Width="186">
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Center" Height="151" Margin="10" Stroke="Black" VerticalAlignment="Center" Width="166"/>
</Grid>
结果是一样的。这是我电脑的问题吗?
最佳答案
您的问题是窗口大小包括 Chrome 大小(窗口边框和诸如关闭/最大最小按钮之类的东西)。
因此您的网格是您请求的大小,但它不适合您请求的窗口大小。
我可以解决您的问题,使输出窗口看起来像这样,窗口大小:
宽度:202 高度:210
在 Windows 8 上
但是,您应该将窗口大小设置为 SizeToContent="WidthAndHeight"
例子:
<Window x:Class="WpfApplication4.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication4"
x:Name="window"
Title="MainWindow"
SizeToContent="WidthAndHeight">
<Grid HorizontalAlignment="Left" Height="171" VerticalAlignment="Top" Width="186">
<Rectangle Fill="#FFF4F4F5" HorizontalAlignment="Left" Height="151" Margin="10,10,0,0" Stroke="Black" VerticalAlignment="Top" Width="166"/>
</Grid>
</Window>
因此您不假设 Chrome 尺寸可能与不同版本的 Windows 不同。
您还应该得到 Snoop .它可以帮助您非常轻松地调试此类问题,当您使用 Shift+Ctrl 将鼠标悬停在控件上时,它会在控件上显示红色边框,因此您实际上可以看到 Grid
延伸到可见窗口之外,同时显示实际的UI 的元素布局
关于调试后 C# WPF 与 IDE 的大小不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15710176/