Question

I have a WPF(4.0 on Win7) datagrid with AlternatingRowBackground when I scroll up row colors remain on place and data are moving when I scroll down colors are moving with data. Is it usual? Is it possible to get rid of this strange behavior?

Grid XAML:

<DataGrid AutoGenerateColumns="True"
                      CanUserAddRows="False"
                      Grid.Row="0" Grid.Column="0"  Name="grdData" 
                      ItemsSource="{Binding Path=.}" SelectionMode="Single"
                      HeadersVisibility="All" 
                      AlternatingRowBackground="LightGray" 
                      />

Note: I have other question about scrolling ( WPF Datagrid scrolls up a bit when scrolled to end ) but I am not sure whether problems are connected.

Was it helpful?

Solution

Add EnableRowVirtualization="False" to your datagrid.

OTHER TIPS

This trigger works with EnableRowVirtualization="True"

<DataGrid Name="dg" Style="{DynamicResource estiloDG}" MinRowHeight="40" GridLinesVisibility="None" 
          HorizontalGridLinesBrush="LightGray" VerticalGridLinesBrush="LightGray"
          BorderBrush="Gray" BorderThickness="1" Foreground="#424242"
          VerticalScrollBarVisibility="Auto" HorizontalScrollBarVisibility="Auto" 
          ItemsSource="{Binding ElementName=Me, Path=ItemsSource, Mode=OneWay}" x:FieldModifier="Private" 
          EnableRowVirtualization="True"
          VirtualizingStackPanel.IsVirtualizing="True"
          VirtualizingStackPanel.VirtualizationMode="Recycling"
          CanUserAddRows="False" CanUserDeleteRows="False" CanUserReorderColumns="True" CanUserSortColumns="True">

    <DataGrid.RowStyle>
        <Style TargetType="{x:Type DataGridRow}">
            <Style.Triggers>
                <Trigger Property="AlternationIndex" Value="0">
                    <Setter Property="Background" Value="White" />
                </Trigger>
                <Trigger Property="AlternationIndex" Value="1">
                    <Setter Property="Background" Value="#FFD0E8D0" />
                </Trigger>                  
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>

<DataGrid.Resources>  
    <Style x:Key="estiloDG" TargetType="{x:Type DataGrid}">
        <Setter Property="BorderBrush" Value="White"/>
        <Setter Property="Background" Value="White" />
        <Setter Property="MinHeight" Value="30" />
        <Setter Property="ColumnHeaderHeight" Value="30" />
        <Setter Property="HeadersVisibility" Value="All" />
        <Setter Property="RowBackground" Value="{StaticResource RowBackgroundBrush}" />
        <Setter Property="AlternationCount" Value="2" />
        <Setter Property="HorizontalGridLinesBrush" Value="{StaticResource DataGridHorizontalLinesBrush}" />
        <Setter Property="VerticalGridLinesBrush" Value="{StaticResource DataGridHorizontalLinesBrush}" />
        <Setter Property="CellStyle" Value="{StaticResource DataGridCellStyle}" />
        <Setter Property="RowValidationErrorTemplate" Value="{StaticResource ErrorTemplate2}" />
        <Setter Property="ColumnHeaderStyle" Value="{StaticResource DatagridColumnHeaderCustomTemplateStyle}" />      
    </Style>
</DataGrid.Resources>

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top