开发者

How to know why my WPF Window is so big?

I have the following Window created in WPF. This is how it's shown:

How to know why my WPF Window is so big?

I have no idea why the screen is shown so big and I don't know how to debug why is it getting so wide.

This is the code related to the problem:

<Window x:Class="Picis.CpCustomize.CustomizeControls.Dialogs.EditIntegerWindow"
MinWidth="350"
SizeToContent="Height"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"
ResizeMode="CanResize" 
ShowInTaskbar="False" 
WindowStartupLocation="CenterScreen" 
WindowState="Normal"
Loaded="OnWindowLoaded">

<!-- Main frame -->
<Grid>

    <!-- Layout -->
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="Auto"/>
        <ColumnDefinition Width="*"/>
    </Grid.ColumnDefinitions>

    <TextBlock VerticalAlignment="Center" MinWidth="100" TextAlignment="Right">
        <TextBlock.Text>
            <Binding Converter="{StaticResource Localizer}" ConverterParameter="General.Value" />
        </TextBlock.Text>
    </TextBlock>

    <TextBox x:Name="valueTextBox" Grid.Column="1" Margin="5" Text="{Binding Path=Value}"/>

    <TextBlock  VerticalAlignment="Center"  Grid.Row="1" Grid.Column="0"  HorizontalAlignment="Right" Visibility="{Binding Path=ShowComments, Converter={StaticResource VisiConv}, ConverterParameter=Collapse}" MinWidth="100" TextAlignment="Right">
            <TextBlock.Text>
                <Binding Converter="{StaticResource Localizer}" ConverterParameter="EditParamDlg.Comment" />
            </TextBlock.Text>
    </TextBlock>

    <TextBox x:Name="commentTextBox" Grid.Row="1" Grid.Column="1" Margin="5" Visibility="{Binding Path=ShowComments, Converter={StaticResource VisiConv}, ConverterParameter=Collapse}" Text="开发者_Go百科{Binding Path=Comment}"/>

    <CheckBox Grid.Row="2" Grid.Column="1" Margin="5" x:Name="isDeletedCheckBox" Visibility="{Binding Path=ShowIsDeleted, Converter={StaticResource VisiConv}, ConverterParameter=Collapse}"
            IsChecked="{Binding Path=IsDeleted}">
        <CheckBox.Content>
            <Binding Converter="{StaticResource Localizer}" ConverterParameter="EditParamDlg.IsDeleted" />
        </CheckBox.Content>
    </CheckBox>

    <UniformGrid Grid.Row="3" Grid.Column="1" Margin="5" HorizontalAlignment="Right" Columns="2">
        <Button  x:Name="okButton" Click="OnOk"  IsDefault="True">
            <Button.Content>
                <Binding Converter="{StaticResource Localizer}" ConverterParameter="General.Ok" />
            </Button.Content>
        </Button>
        <Button x:Name="cancelButton"  Click="OnCancel" Margin="5,0,0,0"  IsCancel="True">
            <Button.Content>
                <Binding Converter="{StaticResource Localizer}" ConverterParameter="General.Cancel" />
            </Button.Content>
        </Button>
    </UniformGrid>

</Grid>
</Window>

My first question is how to debug this issue, the second one is what is happening in this specific scenario.

Thanks in advance.


This question is a duplicate of: Why are my WPF window sizes defaulting to be huge

And according to: Window Size when SizeToContent is not specified the default size when not specified is 60% of the width and 60% of the height of your primary screen.


You explicitly set the window to stretch in width:

SizeToContent="Height"
HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"

This says, "make the window as wide and tall as possible, but then resize to the content of the height"

If you remove that, and set Width and Height to "Auto" (or leave out), you'll likely get what you want. Try just removing all three of those lines (which will leave out alignment, can cause default Width/Height of "Auto" to be used.)


<Window HorizontalAlignment="Stretch"

that is the problem, I guess.


To me it seems this one:

HorizontalAlignment="Stretch"
VerticalAlignment="Stretch"

It stratches the window...


The alignments on a window should not really do anything as there is no container to reference, but you only tell the window to contract to the content in height, i would change that behavior to both dimensions:

SizeToContent="WidthAndHeight"

How to debug this sort of thing? Maybe train your brain to be able to parse XAML and do layout on the fly... i know, not very helpful.


Maybe this is filling existing space (like it is supposed to).

    <ColumnDefinition Width="*"/>
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜