如何根据WPF中的窗口宽度和高度调整控件宽度和高度

时间:2014-07-01 07:32:29

标签: c# wpf

您好我是WPF编程的新手。 根据我的窗户宽度和高度,我有一个宽度和高度的窗口。

TblWind.Height = System.Windows.SystemParameters.FullPrimaryScreenHeight; //(768)
TblWind.Width = System.Windows.SystemParameters.FullPrimaryScreenWidth;   //(1024)

现在我在这个窗口内添加网格,但是想根据窗户的高度和宽度调整其高度和宽度,即4行,每个窗口高度应仅为5% 类似地,每列宽度为2列,应为窗口列的5%

<Grid.RowDefinitions>
        <RowDefinition  Height=".5*" ></RowDefinition>
        <RowDefinition Height=".5*"></RowDefinition>
        <RowDefinition Height=".5*"></RowDefinition>
        <RowDefinition Height=".5*"></RowDefinition>

    </Grid.RowDefinitions>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width=".5*"></ColumnDefinition>
        <ColumnDefinition Width=".5*"></ColumnDefinition>


</Grid.ColumnDefinitions>
    <Label Grid.Row="0" Grid.Column="0" Content="Name:"></Label>
    <Label Grid.Row="1" Grid.Column="0" Content="Name:"></Label>
    <Label Grid.Row="2" Grid.Column="0" Content="Name:"></Label>
    <Label Grid.Row="3" Grid.Column="0" Content="Name:"></Label>
    <TextBox Grid.Row="0" Grid.Column="1" ></TextBox>
    <TextBox Grid.Row="1" Grid.Column="1" ></TextBox>
    <TextBox Grid.Row="2" Grid.Column="1" ></TextBox>
    <TextBox Grid.Row="3" Grid.Column="1" ></TextBox>
</Grid>

但它将窗口宽度分为2个部分,每个部分占宽度的50%。

请帮忙。

1 个答案:

答案 0 :(得分:1)

您的比例尺寸仅意味着所有行高和列高都相同。它不是它测量尺寸的百分比。

<RowDefinition Height="0.5*"/>
<RowDefinition Height-"0.5*"/>

只是意味着第一行的50%大小等于第二行大小的50%。也就是说,他们的高度相等。

与你说的没什么不同:

<RowDefinition Height="2*"/>
<RowDefinition Height="2*"/>

只是意味着第一行大小的两倍等于第二行大小的两倍。

如果您想将网格限制在窗口高度的前20%(4 * 5%= 20%)和最左边10%的窗口宽度(2 * 5%= 10%),那么强制在Grid本身,然后你可以让你的行和列都具有相同的大小。

<Window>

    <Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">

        <Grid.RowDefinitions>
            <RowDefinition Height="1*"/> <!-- 1/5th (20%) of the height -->
            <RowDefinition Height="4*"/>
        </Grid.RowDefinitions>

        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="1*"/> <!-- 1/10th (10%) of the width -->
            <ColumnDefinition Width="9*"/>
        </Grid.ColumnDefinitions>

        <!-- now we have a dedicated cell for your grid... -->
        <Grid Grid.Row="0" Grid.Column="0">

            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>

            <Grid.ColumnDefinitions>
                <ColumnDefinition/>
                <ColumnDefinition/>
            </Grid.ColumnDefinitions>

            <!-- duplicate your content here... -->

        </Grid>

    </Grid>

</Window>
相关问题