Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

Using variables to define Grid.Row in XAML / WPF

$
0
0
While defining a Form using XAML, I am chaining several blocks of fields.
In order to make XAML code more readable and maintenable in the future, I am trying to define a "base" Row for each block, so relative Rows can be used to define each item of the block instead of absolute ones.

Note the differences for:

    < ... Grid.Row="" ...>



in both cases.

Here is an excerpt of my current code:

<!-- Subblock 2 --><!-- Address --><Label Grid.Row="5" Grid.Column="0" Grid.ColumnSpan="2" HorizontalContentAlignment="Right" Content="Address:"/><TextBox Grid.Row="5" Grid.Column="2" Grid.ColumnSpan="4" HorizontalContentAlignment="Center" Text="Address"/>   <TextBox Grid.Row="6" Grid.Column="2" Grid.ColumnSpan="4" HorizontalContentAlignment="Center" Text="Address (cont.)"/>   <TextBox Grid.Row="7" Grid.Column="2" Grid.ColumnSpan="2" HorizontalContentAlignment="Center" Text="Town"/>   <TextBox Grid.Row="7" Grid.Column="4" Grid.ColumnSpan="2" HorizontalContentAlignment="Center" Text="ZIP"/>   <TextBox Grid.Row="8" Grid.Column="2" Grid.ColumnSpan="2" HorizontalContentAlignment="Center" Text="State"/>   <TextBox Grid.Row="8" Grid.Column="4" Grid.ColumnSpan="2" HorizontalContentAlignment="Center" Text="Country"/>


And this is what I would like to have:

<Window.Resources>           <!-- Defines starting rows for each block -->           <System:Int32 x:Key="rowBlock1">0</System:Int32>           <System:Int32 x:Key="rowBlock2">5</System:Int32>   </Window.Resources>    [...]   <!-- Subblock 2 -->   <!-- Address -->   <Label Grid.Row="{StaticResource rowBlock2} + 0" Grid.Column="0" Grid.ColumnSpan="2" HorizontalContentAlignment="Right" Content="Address:"/>   <TextBox Grid.Row="{StaticResource rowBlock2} + 0" Grid.Column="2" Grid.ColumnSpan="4" HorizontalContentAlignment="Center" Text="Address"/>   <TextBox Grid.Row="{StaticResource rowBlock2} + 1" Grid.Column="2" Grid.ColumnSpan="4" HorizontalContentAlignment="Center" Text="Address (cont.)"/>   <TextBox Grid.Row="{StaticResource rowBlock2} + 2" Grid.Column="2" Grid.ColumnSpan="2" HorizontalContentAlignment="Center" Text="Town"/>   <TextBox Grid.Row="{StaticResource rowBlock2} + 2" Grid.Column="4" Grid.ColumnSpan="2" HorizontalContentAlignment="Center" Text="ZIP"/>   <TextBox Grid.Row="{StaticResource rowBlock2} + 3" Grid.Column="2" Grid.ColumnSpan="2" HorizontalContentAlignment="Center" Text="State"/>   <TextBox Grid.Row="{StaticResource rowBlock2} + 3" Grid.Column="4" Grid.ColumnSpan="2" HorizontalContentAlignment="Center" Text="Country"/>


Is is possible to achive that with XAML? Any other tip/suggestion on how to avoid absolute Grid.Row values is also welcomed.

Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>