Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alternate Grid row colors?

Tags:

c#

.net

wpf

xaml

grid

Is it possible to attach a style or define a property in the WPF System.Windows.Controls.Grid in order to alternate the color of the rows(or columns)?

I need that User be able to place a grid placeholder (something similar to CrystalReports)... It can parameter the grid, but the grid (in View) will not contain any data.

like image 311
serhio Avatar asked Mar 09 '12 10:03

serhio


1 Answers

The only way I can think of doing this is by hand using a Rectangle either declared before the row contents or with it's Z order set to push it to the back:

<Grid Background="White">
    <Grid.RowDefinitions>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
        <RowDefinition Height="Auto"/>
    </Grid.RowDefinitions>
    <Rectangle Grid.Row="0" Fill="AliceBlue" />
    <TextBlock Grid.Row="0" Text="Row 1" HorizontalAlignment="Center"/>
    <Rectangle Grid.Row="1" Fill="AntiqueWhite" />
    <TextBlock Grid.Row="1" Text="Row 2" HorizontalAlignment="Center"/>
    <Rectangle Grid.Row="2" Fill="AliceBlue" />
    <TextBlock Grid.Row="2" Text="Row 3" HorizontalAlignment="Center"/>
    <Rectangle Grid.Row="3" Fill="AntiqueWhite" />
    <TextBlock Grid.Row="3" Text="Row 4" HorizontalAlignment="Center"/>
</Grid>

You might be able to use bindings to pass the row number to a converter to return the correct colour rather than hard coding like I have here.

like image 197
ChrisF Avatar answered Sep 27 '22 17:09

ChrisF