What's the meaning of * (asterisk) in XAML ColumnDefinition?

67,291

Solution 1

When you define a column in a WPF grid you can set the width to one of three possible values:

  • A fixed width,
  • Auto – column will become as wide as necessary to fit its children, or
  • * (star) take up any available remaining space

The * is prefixed by a number (default is 1 if no number is specified). The available space is divided among the starred columns in proportion to the prefix number.

If you have this definition

<Grid.ColumnDefinitions>
  <ColumnDefinition Width="0.07*"/>
  <ColumnDefinition Width="0.93*"/>
</Grid.ColumnDefinitions>

The first column will get 7% of the total space available and the second column would get 93%. On the other hand if you had this definition:

<Grid.ColumnDefinitions>
  <ColumnDefinition Width="0.07*"/>
  <ColumnDefinition Width="0.14*"/>
</Grid.ColumnDefinitions>

The first column would get 1/3 and the second 2/3 of the available space.


In your specific case where the width of the grid is 354 and the proportions of the two columns are 40 and 314 you get the following column widths:

First column width = 40/(40 + 314)*354 = 40
Second coulmn width = 314/(40 + 314)*354 = 314

The star width is best used when the width of the grid isn't fixed. When the grid is resized the columns will then scale proportionally as specified by the star widths. In your case the width of the grid is fixed and you could just as easily have used fixed width columns.

If you want a layout where the second column is double the width of the first and the third column is triple the width of the first you need this definition:

<Grid.ColumnDefinitions>
  <ColumnDefinition Width="*"/>
  <ColumnDefinition Width="2*"/>
  <ColumnDefinition Width="3*"/>
</Grid.ColumnDefinitions>

If the total width of the grid is 300 you get column widths 50, 100 and 150. If the total width of the grid is 600 you get column widths 100, 200 and 300. And so on.

Solution 2

Its 0.07 ratio to any other star-width column - i.e. if another ColomnDefinition has a Width of 0.14 then that column is double the width = its all about rations

Solution 3

It creates column sizes using ratios. If you had another definition like <ColumnDefinition Width="0.03*"/> the first column would take up 70% of space and the second one would take up 30%.

Share:
67,291
Shashank
Author by

Shashank

Updated on February 09, 2021

Comments

  • Shashank
    Shashank over 3 years

    What is the meaning of * (asterisk) in the XAML below?

    <ColumnDefinition Width="0.07*"/>
    <Grid Height="100" HorizontalAlignment="Left" 
          Margin="102,134,0,0" 
          Name="grid1" VerticalAlignment="Top" 
          Width="354">
        <Grid.ColumnDefinitions>
            <ColumnDefinition Width="40*" />
            <ColumnDefinition Width="314*" />
        </Grid.ColumnDefinitions>
    </Grid>
    
  • AlejandroAlis
    AlejandroAlis over 3 years
    That the only answer that works.. the secret is in the * after the number. Thanks a lot.