Mahapps.metro: Datagrid Alternating Background Removes row mouse over.

Created on 4 Jun 2013  路  1Comment  路  Source: MahApps/MahApps.Metro

Setting an AlternatingRowBackground prevents the highlight from appearing while moving over a row.

Example to reproduce:

<DataGrid HorizontalAlignment="Left" Height="317" Margin="41,122,0,0" VerticalAlignment="Top" Width="732" 
             Name="grdData" AutoGenerateColumns="False" AlternationCount="2"
             AlternatingRowBackground="#FF282828">
            <DataGrid.Columns>
                <DataGridTextColumn Header="test1" Binding="{Binding}"/>
            </DataGrid.Columns>
        </DataGrid>
Bug

Most helpful comment

I've managed to fix this, but the solution does require a workaround. I've modified the style for DataGridRow as this was setting the Background colour which meant AlternatingRowBackground could not be set from a DataGrid style. I'll create a pull request for this shortly.

For the AlternatingRowBackground to work though, it must be set from a style rather than directly on the DataGrid itself. I don't think there is another alternative. I found a few posts on stackoverflow about it and this seemed to be the only solution.

The style can be set as follows:

c# <DataGrid ItemsSource="{Binding Path=Albums}" Grid.Row="0" AutoGenerateColumns="False"> <DataGrid.Style> <Style TargetType="DataGrid" BasedOn="{StaticResource {x:Type DataGrid}}"> <Setter Property="AlternatingRowBackground" Value="#FF282828"/> </Style> </DataGrid.Style> <DataGrid.Columns> <DataGridTextColumn Header="Title" Binding="{Binding Title}" /> <DataGridTextColumn Header="Artist" Binding="{Binding Artist.Name}" /> <DataGridTextColumn Header="Genre" Binding="{Binding Genre.Name}" /> <DataGridTextColumn Header="Price" Binding="{Binding Price,StringFormat=c}" /> </DataGrid.Columns> </DataGrid>

or you can create a style based on the DataGrid style adding the AlternatingRowBackground so you don't need to do this on every DataGrid.

>All comments

I've managed to fix this, but the solution does require a workaround. I've modified the style for DataGridRow as this was setting the Background colour which meant AlternatingRowBackground could not be set from a DataGrid style. I'll create a pull request for this shortly.

For the AlternatingRowBackground to work though, it must be set from a style rather than directly on the DataGrid itself. I don't think there is another alternative. I found a few posts on stackoverflow about it and this seemed to be the only solution.

The style can be set as follows:

c# <DataGrid ItemsSource="{Binding Path=Albums}" Grid.Row="0" AutoGenerateColumns="False"> <DataGrid.Style> <Style TargetType="DataGrid" BasedOn="{StaticResource {x:Type DataGrid}}"> <Setter Property="AlternatingRowBackground" Value="#FF282828"/> </Style> </DataGrid.Style> <DataGrid.Columns> <DataGridTextColumn Header="Title" Binding="{Binding Title}" /> <DataGridTextColumn Header="Artist" Binding="{Binding Artist.Name}" /> <DataGridTextColumn Header="Genre" Binding="{Binding Genre.Name}" /> <DataGridTextColumn Header="Price" Binding="{Binding Price,StringFormat=c}" /> </DataGrid.Columns> </DataGrid>

or you can create a style based on the DataGrid style adding the AlternatingRowBackground so you don't need to do this on every DataGrid.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

barryBithead picture barryBithead  路  5Comments

bplatypus picture bplatypus  路  3Comments

Coder-Bryan picture Coder-Bryan  路  3Comments

punker76 picture punker76  路  3Comments

bbougot picture bbougot  路  3Comments