向上和向下滚动时,带有AlternatingRowBackground的WPF Datagrid滚动不同

时间:2012-05-16 09:54:47

标签: wpf scroll row wpfdatagrid alternating

我有一个带有AlternatingRowBackground的WPF(Win7上的4.0)数据网格,当我向上滚动行颜色时,当我向下滚动颜色与数据一起移动时数据正在移动。 通常吗?是否有可能摆脱这种奇怪的行为?

网格XAML:

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

注意:我还有关于滚动(WPF Datagrid scrolls up a bit when scrolled to end)的其他问题,但我不确定问题是否已连接。

2 个答案:

答案 0 :(得分:4)

EnableRowVirtualization="False"添加到您的数据网格。

答案 1 :(得分:0)

此触发器适用于 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>