Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does * mean in XAML

What does * mean in XAML. I have a grid of width 400. And divided the grid to 3 columns. What does *.4 mean? I thought it is 40% of the space available. so thought the first 2 columns will get 40% percent each and the rest is taken by the third column. but looks like, the third column is taking 60% and the first two are getting 20% each. How does this work?

<Grid>
    <Grid.ColumnDefinitions>
        <ColumnDefinition Width=".4*"/>
        <ColumnDefinition Width=".4*"/>
        <ColumnDefinition />
    </Grid.ColumnDefinitions>
</Grid>

enter image description here

like image 378
katie77 Avatar asked Sep 14 '11 16:09

katie77


People also ask

What is * and Auto in WPF?

- * means size proportional to grid. Auto means that a column is given as much width as the elements within it require. The width of * sized columns is calculated by allocating space for the Auto , and fixed width columns, and then dividing up the remaining space.

What is the star in WPF?

You can use star sizing when setting the height of rows or width of columns in a Grid to distribute space in a Grid across multiple rows or columns. You can use integer values preceding each '*' (star) to indicate the relative size of rows or columns.

What is XAML stand for?

Extensible Application Markup Language (XAML /ˈzæməl/ ( listen)) is a declarative XML-based language that Microsoft developed for initializing structured values and objects.


1 Answers

Basically, the default is "1*", so what you have above is effectively:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="0.4*" />
    <ColumnDefinition Width="0.4*" />
    <ColumnDefinition Width="1.0*" />
</Grid.ColumnDefinitions>

The Star grid spacing (GridUnitType.Star) proportionally distributes space. In your case, you have a total of 1.8 (1.0 + 0.4 + 0.4), so the first two columns each get 22.2% (0.4/1.8) of the width allocated to them.

To get what you want, you can use:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="0.4*" />
    <ColumnDefinition Width="0.4*" />
    <ColumnDefinition Width="0.2*" />
</Grid.ColumnDefinitions>

This sets the total to 1.0, so each becomes a percentage.

Note that this will give exactly the same result as doing:

<Grid.ColumnDefinitions>
    <ColumnDefinition Width="40*" />
    <ColumnDefinition Width="40*" />
    <ColumnDefinition Width="20*" />
</Grid.ColumnDefinitions>

Since the total proportions are divided up by the total (100) now, still giving 40%, 40%, 20%.

like image 140
Reed Copsey Avatar answered Oct 10 '22 07:10

Reed Copsey