简体   繁体   English

如何分别使WPF Datagrid行前景和替代行前景的颜色?

[英]How to make WPF Datagrid row foreground and alternative row foreground color independtly?

I have a WPF DataGrid , In that I can set DataGrid RowBackground color, alternative RowBackground color independently. 我有一个WPF DataGrid ,因为我可以设置DataGrid RowBackground颜色,单独设置RowBackground颜色。 Likewise I want to set DataGrid RowForeground color, alternative RowForeground color independently. 同样,我想设置DataGrid RowForeground颜色,单独设置RowForeground颜色。

How can I achieve this with simple and optimal way? 如何以简单而最佳的方式实现这一目标?

I am binding DataGrid dynamically, XAML static resource wont help me here. 我正在动态绑定DataGrid,XAML静态资源在这里不会帮我。

you can use the AlternationIndex prop like that: 您可以像这样使用AlternateIndex属性:

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

You can set a style trigger that will change the row's foreground color based on its background color. 您可以设置样式触发器,以根据行的背景色更改行的前景色。

Example, by default, the row's foreground color will be Blue, but if it's Background color is White, then its foreground color will be Red. 例如,默认情况下,该行的前景色为蓝色,但是如果它的背景色为白色,则其前景色为红色。 See below. 见下文。

  <DataGrid ...>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow">
                <Setter Property="TextElement.Foreground" Value="Blue"/>
                <Style.Triggers>
                    <Trigger Property="TextElement.Background" Value="White">
                        <Setter Property="TextElement.Foreground" Value="Red"/>
                    </Trigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
        ...
    </DataGrid>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM