0

I want to apply the following style to certain cells of my grid:

<Border BorderBrush="#808080" BorderThickness="1,1,0,0" Grid.Column="3" >
    <Border BorderBrush="#404040" BorderThickness="1,1,0,0" Width="Auto">
        <Border BorderBrush="#FFFFFF" BorderThickness="0,0,1,1" Width="Auto">
            <Border BorderBrush="#DCE6F4" BorderThickness="0,0,1,1" Width="Auto">
            </Border>
        </Border>
    </Border>
</Border>

Right now i specify the cell in the first line Grid.Colum="3". However to do this for all cells I have to copy paste this like 20 times. Is there any short way to apply my style to all controls or cells of my choice (for example cell 1, , 7, 9 or label1, label 9 and label10?

I am a newbie in wpf, please be gentle :)

2
  • What do you want to place inside the boder? Only labels?
    – Flat Eric
    Jan 4, 2016 at 14:57
  • The basic Grid class doesn't really have 'Cells', just rows and columns. Your might be able to do this with an ItemsControl but that all depends on what you want inside and how you want the layout. Jan 4, 2016 at 15:14

1 Answer 1

2

Assuming you want to place labels inside the grid you could create a ControlTemplate and assign it to the labels you want with Template="{StaticResource BorderLabel}":

<Grid Margin="5">
  <Grid.ColumnDefinitions>
    <ColumnDefinition Width="*" />
    <ColumnDefinition Width="*" />
  </Grid.ColumnDefinitions>

  <Grid.Resources>
    <ControlTemplate TargetType="Label" x:Key="BorderLabel">
      <Border BorderBrush="#808080" BorderThickness="1,1,0,0" >
        <Border BorderBrush="#404040" BorderThickness="1,1,0,0" Width="Auto">
          <Border BorderBrush="#FFFFFF" BorderThickness="0,0,1,1" Width="Auto">
            <Border BorderBrush="#DCE6F4" BorderThickness="0,0,1,1" Width="Auto">
              <ContentControl Content="{TemplateBinding Content}" />
            </Border>
          </Border>
        </Border>
      </Border>
    </ControlTemplate>

  </Grid.Resources>

  <Label Grid.Column="0" Content="MyText" />
  <Label Grid.Column="1" Content="MyText" Template="{StaticResource BorderLabel}" />
</Grid>

And this is how it looks:

Result

1
  • Thanks a lot, that's exactly what I meant. Jan 5, 2016 at 14:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.