2

当我向上滚动时,我有一个带有 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 在滚动到结束时会向上滚动一点),但我不确定是否存在问题。

4

2 回答 2

6

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

于 2012-05-16T11:29:35.263 回答
-1

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

于 2015-01-14T07:59:59.173 回答