简体   繁体   English

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

[英]How to adjust control width and height according to windows width and height in WPF

Hi I am new to WPF programming. 嗨,我是WPF编程的新手。 I am having a window with its width and height according to my window width and height. 我有一个窗口,其宽度和高度根据我的窗口的宽度和高度而定。

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

Now i am adding Grid inside this window but want to adjust its height and width on the basis of windows height and width ie 4 rows and each should take only 5% of windows height Similarly 2 columns each width should be 5% of windows column 现在我要在此窗口中添加Grid,但要根据窗口的高度和宽度(即4行)调整其高度和宽度,每行应仅占窗口高度的5%类似地,两列的每个宽度应为窗口列的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>

But it is dividing window width in 2 parts with each taking 50% of width. 但是它将窗口宽度分成两部分,每部分占宽度的50%。

Please help. 请帮忙。

Your proportional sizing just means that all row heights and column heights will be the same. 您的比例大小只是意味着所有行高和列高都将相同。 It's not a percentage of it's measured size. 它不是测量尺寸的百分比。

ie

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

Just means that 50% of the size of the first row is equal to 50% of the size of the second row. 只是意味着第一行的大小的50%等于第二行的大小的50%。 That is, their heights are equal. 即,它们的高度相等。

No different than if you said: 与您说的没什么不同:

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

just means that twice the size of the first row is equal to twice the size of the second row. 只是意味着第一行的两倍等于第二行的两倍。

If you want to constrain your grid to the top 20% of your window height (4 * 5% = 20%) and the left-most 10% of your window width (2 * 5% = 10%), then force that in a Grid itself, and then you can just allow your rows and columns to all have equal sizing. 如果要将网格限制为窗口高度的顶部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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM