WPF 星星有什么作用(Width="100*")

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1768293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 21:14:26  来源:igfitidea点击:

What does the WPF star do (Width="100*")

wpfxamlgridsize

提问by Shimmy Weitzhandler

What does exactly the star in size terms in WPF mean?

WPF 中的星星在大小方面究竟是什么意思?

回答by Edward

In a WPF Grid, Width="*"or Height="*"means proportional sizing.
For example: to give 30% to column 1 and 70% to column 2 -

在 WPF 网格中,Width="*"Height="*"表示按比例调整大小。
例如:将 30% 分配给第 1 列,将 70% 分配给第 2 列 -

<ColumnDefinition Width="3*" />
<ColumnDefinition Width="7*" />

enter image description here

在此处输入图片说明

And likewise for rows -

同样对于行 -

<RowDefinition Height="3*" />
<RowDefinition Height="7*" />

The numbers do not have to be integers.
If the Width for RowDefinition (Height for ColumnDefinition) is omitted, 1* is implied.
In this example, column 1 is 1.5 times wider than column 2 -

数字不必是整数。
如果省略 RowDefinition 的宽度(ColumnDefinition 的高度),则隐含 1*。
在此示例中,第 1 列比第 2 列宽 1.5 倍 -

<ColumnDefinition Width="1.5*" />
<ColumnDefinition />

Column 1: 1.5*, Column 2 1* (implied)

第 1 列:1.5*,第 2 列 1*(隐含)

You can mix auto-fit and fixed widths with * (proportional) widths; in that case the * columns are apportioned to the remainder after the auto-fit and fixed widths have been calculated -

您可以将自动调整宽度和固定宽度与 *(比例)宽度混合使用;在这种情况下, * 列在计算自动适应和固定宽度后分配到其余部分 -

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />  <!-- Auto-fit to content, 'Hi' -->
    <ColumnDefinition Width="50.5" />  <!-- Fixed width: 50.5 device units) -->
    <ColumnDefinition Width="69*" />   <!-- Take 69% of remainder -->
    <ColumnDefinition Width="31*"/>    <!-- Take 31% of remainder -->
</Grid.ColumnDefinitions>
<TextBlock Text="Hi" Grid.Column="0" />

enter image description here

在此处输入图片说明

回答by Mark Carpenter

If you have 2 columns like this:

如果你有 2 列是这样的:

<ColumnDefinition Width="10*"/>
<ColumnDefinition Width="*"/>

it means that the first column is 10x wider than the second. It's like saying "10 parts column 1, and 1 part column 2."

这意味着第一列比第二列宽 10 倍。这就像说“第 1 列 10 份,第 2 列 1 份”。

The cool thing about this is that your columns will resize proportionally. Other options are:

很酷的一点是,您的列将按比例调整大小。其他选项是:

//Take up as much space as the contents of the column need
<ColumnDefinition Width="Auto"/>
//Fixed width: 100 pixels
<ColumnDefinition Width="100"/>

Hope that helps!

希望有帮助!

回答by Rikin Patel

we take following example.....

我们以下面的例子.....

one grid and has 3 columns and each contain one button of size 100.

一个网格,有 3 列,每列包含一个大小为 100 的按钮。

enter image description here

在此处输入图片说明

XAML Code is...

XAML 代码是...

    <Grid x:Name="LayoutRoot" Width="600">
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width="3*" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="*" />
    </Grid.ColumnDefinitions>
    <Button Content="Button" Height="23" HorizontalAlignment="Left" Margin="10,10,0,0" Name="button1" VerticalAlignment="Top" Width="100" />
    <Button Content="Button1" Height="23" HorizontalAlignment="Left" Margin="0,10,0,0" Name="button2" VerticalAlignment="Top" Width="100" Grid.Column="1" />
    <Button Content="Button2" Height="23" HorizontalAlignment="Left" Margin="0,10,0,0" Name="button3" VerticalAlignment="Top" Width="100" Grid.Column="2" />
</Grid>

But actually its size is....

但实际上它的大小是......

<Grid.ColumnDefinitions>
        <ColumnDefinition Width="375" />
        <ColumnDefinition Width="Auto" />
        <ColumnDefinition Width="125" />
</Grid.ColumnDefinitions>

Conclusion:

结论:

Total size of grid is 600

网格的总大小为 600

"Auto" : Column is re-size with it's contains. (2nd column has button of width 100)

"Auto" : 列被重新调整大小。(第二列有宽度为 100 的按钮)

"*" : 1st column width is 3x of 3rd column.

“*”:第一列宽度是第三列的 3 倍。

回答by Dave

In addition, you can leave out the "*" if that's the element of unit size. So using Pwninstein's code example, it would just be:

此外,如果这是单位大小的元素,您可以省略“*”。所以使用 Pwninstein 的代码示例,它只是:

<ColumnDefinition Width="10*/>
<ColumnDefinition/>