在触发器上设置Wpf AlternatingRowBackground样式

时间:2012-01-24 12:36:17

标签: wpf xaml datagrid

我想根据dataTrigger更改AlternatingRowBackground颜色。 我从IDE收到一个错误,它不会构建(参见下面的xaml)。

错误:在“System.Windows.Controls.DataGridRow”类型上找不到样式属性“AlternatingRowBackground”

的Xaml

 <Style TargetType="{x:Type DataGridRow}">
            <Setter Property="AlternatingRowBackground" Value="Beige" />
            <Style.Triggers>
                <Trigger Property="IsMouseOver" Value="True">
                    <Setter Property="Background" Value="{StaticResource SelectedRowBackgroundBrush}" />
                </Trigger>
                <DataTrigger Binding="{Binding BondType}" Value="P">
                    <Setter Property="Background" Value="Pink"/>
                    <Setter Property="AlternatingRowBackground" Value="LightPink" />
                </DataTrigger>
            </Style.Triggers>
        </Style>

任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:3)

您的样式定位于 DataGridRow ,但“AlternatingRowBackground”是 DataGrid 的属性。

因此,您需要为DataGrid和DataGridRow提供单独的样式。在DataGrid样式中设置AlternatingRowBackground:

<Style x:Key="DataGridStyle" TargetType="{x:Type DataGrid}">
    <Setter Property="AlternatingRowBackground" Value="Beige" />
    ...
</Style>

<Style x:Key="DataGridDemoRowStyle" TargetType="{x:Type DataGridRow}">
     <Style.Triggers>
            <Trigger Property="IsMouseOver" Value="True">
                <Setter Property="Background" Value="{StaticResource SelectedRowBackgroundBrush}" />
            </Trigger>
            <DataTrigger Binding="{Binding BondType}" Value="P">
                <Setter Property="Background" Value="Pink"/>
            </DataTrigger>
     </Style.Triggers>
</Style>
相关问题