WPF DataGrid交替行着色

56

我已经尝试了这个方法,但是没有成功。

 <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>
5个回答

53

最终,这是我为通用设置交替行颜色使用的代码。

<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>

谢谢,我使用这个是因为当条件满足时,我想要用自己的颜色覆盖交替颜色。 - rosta
1
有一个内置属性可用于此目的:“AlternatingRowBackground”,您可以将“Background”设置为一种颜色,“AlternatingRowBackground”设置为第二种颜色,不需要使用触发器。 - IFink

45

如果还没有设置,您需要设置DataGrid的AlternationCount属性:

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

你还应该检查DataGridRow中是否使用了Foreground属性。尝试设置Background属性来测试交替变化的效果。


1
我在四处搜索时发现了这个,当时就像“啊哈!”。:) 不过还是谢谢! - Robin Maben
12年后,现在已经发生了变化。设置“AlternatingRowBackground”属性会自动将“AlternationCount”属性设置为2。 - CodeJunkie

35

尝试像这样设置交替背景:

  AlternationCount="2" AlternatingRowBackground="Bisque"

这是我找到的最佳解决方案,因为它简洁明了。 - Vectoria

5

试试这个

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

4
此答案已经被@Th3G33k提出建议。 - Sunny Patel

3

最终我使用了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>

感谢您详细解答。这正是我所需要的。 - user3856437

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接