Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Silverlight (3.0): How to add cell padding to a Grid?

How to add easily a cell padding for a Grid in Silverlight? To set Margins for each cell looks very noisy.

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

<TextBlock Text="Type:" Grid.Column="0" Grid.Row="0"></TextBlock>
<ComboBox Grid.Column="1" Grid.Row="0"></ComboBox>
<TextBlock Text="Length:" Grid.Column="0" Grid.Row="1"  ></TextBlock>
<TextBox  Grid.Column="1"  Grid.Row="1"></TextBlock>

like image 705
Sven Sönnichsen Avatar asked Dec 07 '09 17:12

Sven Sönnichsen


2 Answers

Someone will probably crucify me for the ugliness of this solution, but you can add Rows and Columns with Height and Width set to twice your padding values in between the actual rows and columns that contain data:

<Grid> 
<Grid.RowDefinitions>
    <RowDefinition Height="Auto" />
    <RowDefinition Height="4" />
    <RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<Grid.ColumnDefinitions>
    <ColumnDefinition Width="Auto" />
    <ColumnDefinition Width="4" />
    <ColumnDefinition Width="*"  />
</Grid.ColumnDefinitions>
<TextBlock Text="test" Grid.Column="0" Grid.Row="0" />
<TextBlock Text="test" Grid.Column="0" Grid.Row="2" />
<TextBlock Text="test" Grid.Column="2" Grid.Row="0" />
<TextBlock Text="test" Grid.Column="2" Grid.Row="2" />
</Grid>
like image 69
Klay Avatar answered Dec 19 '22 19:12

Klay


I personally prefer to use margins. To clean it up a little bit, you can refactor them into styles. You could even go one step farther and use an Implicit style manager.

If you really wanted something clean, you could make a padding attached property that would handle the grid loaded event and then set the margins of all of the children.

like image 20
Jacob Adams Avatar answered Dec 19 '22 19:12

Jacob Adams