WPF DataGrid列交替着色

时间:2010-10-24 16:02:19

标签: wpf datagrid styles wpftoolkit

如果我还想使用AlternatingRowBackground属性,如何在DataGrid中使列着色? 我有一些想法,但它不起作用:(。

<de:DataGrid Name="dataGrid1"
                             AlternationCount="2"
                             AlternatingRowBackground="Salmon"
                             >
                    <de:DataGrid.Columns>
                        <de:DataGridTextColumn Binding="{Binding Path=Phrase}"
                                               Header="Phrase">
                            <de:DataGridTextColumn.ElementStyle>
                                <Style TargetType="{x:Type TextBlock}">
                                    <Style.Triggers>
                                        <Trigger Property="ItemsControl.AlternationIndex" Value="0">
                                            <Setter Property="Background" Value="Green"></Setter>
                                        </Trigger>
                                        <Trigger Property="ItemsControl.AlternationIndex" Value="1">
                                            <Setter Property="Background" Value="Red"></Setter>
                                        </Trigger>

                                    </Style.Triggers>
                                </Style>
                            </de:DataGridTextColumn.ElementStyle>
                        </de:DataGridTextColumn>
                     </de:DataGrid.Columns>
                </de:DataGrid>

也许有人知道工作解决方案?感谢。

1 个答案:

答案 0 :(得分:0)

您正在错误的控件中搜索AlternationIndex属性。此属性属于DataGridRow。

<DataGrid ItemsSource="{Binding}" AlternationCount="2" AutoGenerateColumns="False" AlternatingRowBackground="Salmon">
            <DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding Path=Phrase}" Header="Phrase">
                    <DataGridTextColumn.ElementStyle>
                        <Style TargetType="{x:Type TextBlock}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding AlternationIndex, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="0">
                                    <Setter Property="Background" Value="Green"></Setter>
                                </DataTrigger>
                                <DataTrigger Binding="{Binding AlternationIndex, RelativeSource={RelativeSource AncestorType=DataGridRow}}" Value="1">
                                    <Setter Property="Background" Value="Red"></Setter>
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </DataGridTextColumn.ElementStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>
          </DataGrid>