WPF 星号的作用是什么(宽度=“100*”)

发布于 2024-08-11 20:26:01 字数 25 浏览 1 评论 0原文

WPF 中大小的星号到底是什么意思?

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

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

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

发布评论

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

评论(4

南城追梦 2024-08-18 20:26:01

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

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

在此处输入图像描述

对于行也是如此 -

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

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

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

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

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

<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" />

在此处输入图像描述

In a WPF Grid, Width="*" or Height="*" means proportional sizing.
For example: to give 30% to column 1 and 70% to column 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 -

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

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

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

〗斷ホ乔殘χμё〖 2024-08-18 20:26:01

如果您有 2 列,如下所示:

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

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

这样做的一个很酷的事情是您的列将按比例调整大小。其他选项是:

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

希望有帮助!

If you have 2 columns like this:

<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."

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!

罪歌 2024-08-18 20:26:01

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

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

在此处输入图像描述

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>

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

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

结论:

  • 网格的总大小为 600

  • Auto :列根据其内容调整大小。 (第二列有宽度为 100 的按钮)

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

we take following example.....

A grid has 3 columns each containing one button of width 100.

enter image description here

XAML Code is...

    <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>

Conclusions:

  • Total size of grid is 600

  • Auto : Column is resizes based on it's contents. (2nd column has button of width 100)

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

神妖 2024-08-18 20:26:01

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

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

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:

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