我在Visual Studio 2010中创建了简单的界面。当我启动应用程序时,一切都变得更大。 屏幕截图上的红色方块表示启动的应用程序中按钮的大小(右侧)。 我错过了什么吗? 怎么解决?
PS在“Microsoft Expression Blend 4”中,相同的项目与编辑器中的项目大小完全相同。
<Window x:Class="WpfControlReview.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="120" Width="256" ResizeMode="NoResize"> <Grid> <Button HorizontalAlignment="Stretch" Name="button1" VerticalAlignment="Stretch"> <StackPanel HorizontalAlignment="Stretch" Name="stackPanel1" VerticalAlignment="Center"> <Label IsHitTestVisible="False" Content="Select your options and press to commit" Name="label1" HorizontalContentAlignment="Center" FontSize="12" /> <StackPanel Name="stackPanel2" Orientation="Horizontal"> <Expander Header="Color" Name="expander1"> </Expander> <Expander Header="Make" Name="expander2"> </Expander> <Expander Header="Payment" Name="expander3"> </Expander> </StackPanel> </StackPanel> </Button> </Grid> </Window>
I made simple interface in Visual Studio 2010. When I launched application everything is bigger. Red square on screenshot represents size of the Button in launched application (On the right). Am I missing something? How to fix it?
P.S. In "Microsoft Expression Blend 4" the same project is exactly the same size as in editor.
<Window x:Class="WpfControlReview.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" mc:Ignorable="d" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" Height="120" Width="256" ResizeMode="NoResize"> <Grid> <Button HorizontalAlignment="Stretch" Name="button1" VerticalAlignment="Stretch"> <StackPanel HorizontalAlignment="Stretch" Name="stackPanel1" VerticalAlignment="Center"> <Label IsHitTestVisible="False" Content="Select your options and press to commit" Name="label1" HorizontalContentAlignment="Center" FontSize="12" /> <StackPanel Name="stackPanel2" Orientation="Horizontal"> <Expander Header="Color" Name="expander1"> </Expander> <Expander Header="Make" Name="expander2"> </Expander> <Expander Header="Payment" Name="expander3"> </Expander> </StackPanel> </StackPanel> </Button> </Grid> </Window>
最满意答案
我不确定这是不是一个bug,但这里的基本思想是你已经为你的窗口指定了一个默认大小,但由于控件想要更大(无论出于何种原因),它会自动调整窗口大小。 可能是一个错误,Visual Studio如何使用指定的参数计算设计器中的窗口边框大小,因为它错误地显示它比实际启动应用程序时小一点。
我建议你尽可能避免指定高度/宽度。 而是将Window设置为SizeToContent = WidthAndHeight。 如果您需要使用控件的大小,请直接在控件上设置边距或MinWidth / MinHeight。
I'm not sure if this is a bug or not, but the basic idea here is that you have specifed a default size for your window, but because the control wants to be bigger (for whatever reason) it will automatically resize the window. Could be a bug how Visual Studio calculates window border size in the designer with the specified parameters as it's incorrectly showing it as a bit smaller than when you actually launch your application.
I would rather suggest that you avoid specifying heights/widths if possible. Instead set the Window to SizeToContent=WidthAndHeight. If you need to play with the size of your controls use Margins or MinWidth/MinHeight set directly on your controls.
更多推荐
发布评论