Reputation: 12287
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>
Upvotes: 3
Views: 1939
Reputation: 236
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>
Upvotes: 5
Reputation: 4701
You can subclass Grid and add any behavior you need.
Here is an implementation of AutoGrid
that automatically inserts new rows for any AutoGridEndRow
object as well as whenever Grid.Row
is larger than current definitions count.
Usage as below:
<Window xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:my="clr-namespace:WpfApplication1"
Title="Window1" Height="300" Width="300">
<my:AutoGrid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="65" />
<ColumnDefinition Width="*" />
</Grid.ColumnDefinitions>
<Label Content="Label1" />
<TextBox Grid.Column="1" />
<my:AutoGridEndRow />
<Label Content="Label1" />
<TextBox Grid.Column="1" />
</my:AutoGrid>
</Window>
Upvotes: 2
Reputation: 34359
No, there isn't a short hand method for this, but you could write your own solution, or use a framework where someone already has.
For example, the CODE framework allows you to define markup as shown in Listing 6 here. This uses a custom panel to greatly simplify the definition of common editing forms.
You could download the source and have a look at their implementation and tailor it for your needs.
Upvotes: 4