Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any shorthand for WPF column and row definitions?

Tags:

wpf

I have a simple grid layout which is just hosting several labels and textboxes. Labels in the first column, boxes in the second.

Whenever I add a new box, I have to add an empty <RowDefinition /> in to my <Grid.RowDefinitions> block to allow it to occupy a new row. Since I don't have any style attached to these rows, is there some sort of shorthand that would prevent having to to this?

<Grid.ColumnDefinitions>
  <ColumnDefinition Width="65" />
  <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
  <RowDefinition />
  <RowDefinition />
  <RowDefinition />
  <RowDefinition />
  <RowDefinition />
  <RowDefinition />
  <RowDefinition />
  <RowDefinition />
  <RowDefinition />
</Grid.RowDefinitions>
like image 369
Collin Avatar asked Jan 20 '26 13:01

Collin


1 Answers

You could use an Attached Property to create a shorthand syntax on your own.

I've created a sample here: https://github.com/thomasclaudiushuber/Wpf-Grid-Extensions

It allows you to write this:

<Grid local:GridExtensions.Structure="*,100|200,*">

</Grid>

And behind the scenes it creates this:

<Grid>
  <Grid.RowDefinitions>
    <RowDefinition Height="*"/>
    <RowDefinition Height="100"/>
  </Grid.RowDefinitions>
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="200"/>
    <ColumnDefinition Width="*"/>
  </Grid.ColumnDefinitions>

</Grid>
like image 72
Thomas Claudius Huber Avatar answered Jan 23 '26 06:01

Thomas Claudius Huber



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!