为什么我的 WPF 窗口大小默认很大

发布于 2024-09-07 18:28:51 字数 3425 浏览 1 评论 0原文

我有一个带有一些表单的 wpf 应用程序。在设计时它们很小,并且没有设置为自动大小。然而在运行时它们是巨大的,即使其中没​​有任何内容可以使它们变大。

为什么会发生这种情况?

这是其中一种表格

<Window x:Class="SuperPluginPicker"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:tree="clr-namespace:Aga.Controls.Tree;assembly=Aga.Controls" 
             mc:Ignorable="d" 
             d:DesignHeight="296" d:DesignWidth="634" Title="Plugin Selector" WindowStartupLocation="CenterOwner">
    <Grid>
        <DockPanel LastChildFill="true">
            <StackPanel DockPanel.Dock="Bottom" Height="30" Orientation="Horizontal">
                <Button Content="Ok" Name="btnOk" Click="btnOk_Click"></Button>
                <Button Content="Cancel" Name="btnCancel" Click="btnCancel_Click"></Button>

            </StackPanel>
            <StackPanel DockPanel.Dock="Right">
                <Label Content="Selected Plugins"></Label>
            <ListBox Name="lstSelectedPlugins"  Width="200">
                <ListBox.ItemTemplate>



                    <DataTemplate>



                        <Label Content="{Binding Name}" />

                    </DataTemplate>

                </ListBox.ItemTemplate>

            </ListBox>
            </StackPanel>
            <StackPanel DockPanel.Dock="Right" Width="100" VerticalAlignment="Center">
                <Button Content="Add" Name ="btnAdd" Click="btnAdd_Click"></Button>
                <Button Content="Remove" Name="btnRemove" Click="btnRemove_Click"></Button>
                <Button Content="Remove All" Name="btnRemoveAll" Click="btnRemoveAll_Click"></Button>
            </StackPanel>

            <tree:TreeList  x:Name="pluginTree">
                <tree:TreeList.View>
                    <GridView x:Name="treeGrid">
                        <GridView.Columns>

                            <GridViewColumn Width="Auto" Header="Name">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <tree:RowExpander/>

                                            <TextBlock Text="{Binding Name}"></TextBlock>
                                        </StackPanel>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Header="Author" Width="Auto"  DisplayMemberBinding="{Binding Author}"/>
                            <GridViewColumn Header="Description" Width="Auto" DisplayMemberBinding="{Binding Type}"/>


                        </GridView.Columns>
                    </GridView>
                </tree:TreeList.View>
            </tree:TreeList>
        </DockPanel>
    </Grid>
</Window>

I have a wpf application with a few forms. At design time they are small, and they are not set to auto size. However at run time they are giant, even with no content in them to make them big.

Why is this happening?

Here is one of the forms

<Window x:Class="SuperPluginPicker"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:tree="clr-namespace:Aga.Controls.Tree;assembly=Aga.Controls" 
             mc:Ignorable="d" 
             d:DesignHeight="296" d:DesignWidth="634" Title="Plugin Selector" WindowStartupLocation="CenterOwner">
    <Grid>
        <DockPanel LastChildFill="true">
            <StackPanel DockPanel.Dock="Bottom" Height="30" Orientation="Horizontal">
                <Button Content="Ok" Name="btnOk" Click="btnOk_Click"></Button>
                <Button Content="Cancel" Name="btnCancel" Click="btnCancel_Click"></Button>

            </StackPanel>
            <StackPanel DockPanel.Dock="Right">
                <Label Content="Selected Plugins"></Label>
            <ListBox Name="lstSelectedPlugins"  Width="200">
                <ListBox.ItemTemplate>



                    <DataTemplate>



                        <Label Content="{Binding Name}" />

                    </DataTemplate>

                </ListBox.ItemTemplate>

            </ListBox>
            </StackPanel>
            <StackPanel DockPanel.Dock="Right" Width="100" VerticalAlignment="Center">
                <Button Content="Add" Name ="btnAdd" Click="btnAdd_Click"></Button>
                <Button Content="Remove" Name="btnRemove" Click="btnRemove_Click"></Button>
                <Button Content="Remove All" Name="btnRemoveAll" Click="btnRemoveAll_Click"></Button>
            </StackPanel>

            <tree:TreeList  x:Name="pluginTree">
                <tree:TreeList.View>
                    <GridView x:Name="treeGrid">
                        <GridView.Columns>

                            <GridViewColumn Width="Auto" Header="Name">
                                <GridViewColumn.CellTemplate>
                                    <DataTemplate>
                                        <StackPanel Orientation="Horizontal">
                                            <tree:RowExpander/>

                                            <TextBlock Text="{Binding Name}"></TextBlock>
                                        </StackPanel>
                                    </DataTemplate>
                                </GridViewColumn.CellTemplate>
                            </GridViewColumn>
                            <GridViewColumn Header="Author" Width="Auto"  DisplayMemberBinding="{Binding Author}"/>
                            <GridViewColumn Header="Description" Width="Auto" DisplayMemberBinding="{Binding Type}"/>


                        </GridView.Columns>
                    </GridView>
                </tree:TreeList.View>
            </tree:TreeList>
        </DockPanel>
    </Grid>
</Window>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

我的奇迹 2024-09-14 18:28:51

如果您使用以下代码创建

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />

那么当您从 Visual Studio 运行该代码时,它将显示一个与您在问题中放入的窗口大小相同的窗口。所以这不是你明确做的任何事情。

您遇到的是 WPF Window 的默认计算大小。对于不同的结果,请在窗口上指定 SizeToContent 参数,如 paxdiablo 指出的那样,或使用 MinHeightMaxHeight 显式设置窗口的大小>、HeightMinWidthMaxWidth 和/或 Width 属性。

If you create a <Window/> with this code:

<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" />

Then when you run that from Visual Studio it will display a window the same size as the one you put in your question. So this isn't anything you explicitly did.

What you've encountered is the default computed size of a WPF Window. For different results, specify a SizeToContent parameter on the Window, as paxdiablo pointed out, or explicitly set the size of your window with the MinHeight, MaxHeight, Height, MinWidth, MaxWidth, and/or Width properties.

混浊又暗下来 2024-09-14 18:28:51

为什么不直接

Height="296" Width="634"

Window 定义中添加一个:呢?我不确定默认尺寸从何而来,但我已经在窗口上设置了特定尺寸来解决这个问题。然后我在应用程序启动代码中动态调整大小。

我希望 WPF 具有类似 pack 的功能,它可以根据子控件的首选大小递归地设置控件(Swing 和 wxPython 都有此功能),所以您可能想要正在寻找那个。


实际上,经过一些搜索,发现了 Window.SizeToContent 属性可能就是您正在寻找的。它的默认值为 Manual,但是,如果您将其设置为其他值之一(WidthHeightWidthAndHeight ),您可能会看到更好的演示。

事实上,我只是偷偷地走到 Windows 笔记本电脑上并输入:

SizeToContent="WidthAndHeight"

进入 Window 定义,它看起来大小完美。因此,我建议尝试一下,看看它是否可以解决(或至少解决)您的问题。

Why don't you just add a:

Height="296" Width="634"

to the Window definition? I'm not sure where the default size comes from but I've set specific sizes on my windows to get around this. Then I dynamically resize in the application startup code.

I'm hoping that WPF will have a pack-like feature where it recursively sets controls based on the preferred sizes of the sub-controls (Swing and wxPython both had this feature) so you may want to go looking for that.


Actually, a bit of searching has turned up the Window.SizeToContent property which may be what you're looking for. Its default is Manual but, if you set it to one of the other values (Width, Height or WidthAndHeight), you may see a better presentation.

In fact, I just nicked over to the Windows laptop and entered:

SizeToContent="WidthAndHeight"

into the Window definition and it looks perfectly sized. So, I'd suggest giving that a try and seeing if it fixes (or at least works around) your problem.

还在原地等你 2024-09-14 18:28:51

在设计中,您会看到很小,因为您具有窗口的混合设计属性。

d:DesignHeight="296" d:DesignWidth="634"

由于网格是动态布局,它将拉伸到窗口的宽度和宽度。高度。要限制窗口大小,您需要提及高度和宽度。窗口的宽度属性。

In design you are seeing small because you have blend design properties for the window.

d:DesignHeight="296" d:DesignWidth="634"

Since Grid is a dynamic layout it will be stretch to the window's width & height. To restrict you window size you need to mention Height & Width properties to the window.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文