wpf数据网格交替行着色

wpf数据网格交替行着色,wpf,xaml,wpf-controls,styles,wpfdatagrid,Wpf,Xaml,Wpf Controls,Styles,Wpfdatagrid,我试过这种方法。。没有运气 <Style TargetType="{x:Type DataGridRow}"> <Style.Triggers> <Trigger Property="ItemsControl.AlternationIndex" Value="0"> <Setter Property="Foreground" Value="Red" /> </Trigger> <

我试过这种方法。。没有运气

 <Style TargetType="{x:Type DataGridRow}">
  <Style.Triggers>
      <Trigger Property="ItemsControl.AlternationIndex" Value="0">
          <Setter Property="Foreground" Value="Red" />
     </Trigger>
  </Style.Triggers>
</Style>

有没有办法获取行索引? 我甚至试过

<DataTrigger Binding="{Binding AlternationIndex}" Value="0">
    <Setter Property="Foreground" Value="Green"></Setter>
</DataTrigger>

除非已经完成,否则必须设置DataGrid的AlternationCount属性:

<DataGrid AlternationCount="2"
          ... />


您还应该检查前台属性是否用于DataGridRow中的任何控件。试着设置Background属性来测试替换内容。

最后,这就是我通常设置替换行颜色的结果

<Style TargetType="{x:Type DataGrid}">
    <Setter Property="Background" Value="#FFF" />
    <Setter Property="AlternationCount" Value="2" />
</Style>

 <Style TargetType="{x:Type DataGridRow}">
    <Style.Triggers>
        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
            <Setter Property="Background" Value="#CCC"></Setter>
        </Trigger>
        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
            <Setter Property="Background" Value="#EEE"></Setter>
        </Trigger>
    </Style.Triggers>
</Style>

尝试如下设置交替背景:

  AlternationCount="2" AlternatingRowBackground="Bisque"
试试这个

  <DataGrid AlternationCount="2"
            AlternatingRowBackground="Salmon" ........

最后,我使用了Robin Maben和Th3G33k解决方案的组合,因为我希望在满足某些条件时,使用我自己的替代颜色。
谢谢两位

<DataGrid x:Name="gridCustomerOrderItems" HorizontalAlignment="Stretch" 
                  VerticalAlignment="Stretch" AutoGenerateColumns="False"
                  AlternationCount="2"
                  IsReadOnly="True" CanUserReorderColumns="True"
                      ScrollViewer.CanContentScroll="True"
                      ScrollViewer.VerticalScrollBarVisibility="Auto"
                      ScrollViewer.HorizontalScrollBarVisibility="Auto">

                <DataGrid.RowStyle>
                    <Style TargetType="DataGridRow">
                        <Style.Triggers>
                            <!--first alteraniting colour-->
                            <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                <Setter Property="Background" Value="#EEE"></Setter>
                            </Trigger>
                            <!--then override with my own colour-->
                            <DataTrigger Binding="{Binding InvoiceSet}" Value="True">
                                <Setter Property="Background" Value="Green"></Setter>
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </DataGrid.RowStyle>


我四处搜索,发现了这个,我想“啊,是的!”:)谢谢你!。。这是我遇到的最好的解决方案,因为它简明扼要。这个答案已经由提出。谢谢,我使用这个,因为我希望在满足条件时用我自己的替换颜色覆盖:有一个内置属性用于此目的:“AlternatingRowBackground”,您可以将“Background”设置为一种颜色,然后“AlternatingRowBackground”为第二种颜色,无需此触发器。