本文介绍了WPF DataGrid AlternatingRowBackground和RowStyle优先级的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在 AlternatingRowBackground 之后应用我的 RowStyle ?我希望具有 IsOrange 作为 true 的项目具有 Orange

XAML:

 < DataGrid名称= g 
AlternatingRowBackground = Blue
AlternationCount = 2
...
SelectionMode = Single>
< DataGrid.RowStyle>
< Style TargetType = DataGridRow>
< Style.Triggers>
< DataTrigger Binding = {Binding IsOrange} Value = Y>
< Setter Property = Background Value = Orange />
< / DataTrigger>
< /Style.Triggers>
< / Style>
< /DataGrid.RowStyle>
...
< / DataGrid>


解决方案

这不是错误。在样式中,您无法覆盖为交替行设置的本地值。这就是为什么这不起作用

 < DataGrid AlternatingRowBackground = Blue 

但是,如果您将 AlternatingRowBackground 设置为样式您可以

 < DataGrid.Style> 
< Style TargetType = DataGrid>
< Setter Property = AlternatingRowBackground Value = Blue />
< / Style>
< /DataGrid.Style>

感谢。


How do I make my RowStyle to get applied after AlternatingRowBackground? I want items, having IsOrange as true to have Orange background regardless of alternating row background, which isn't the case currently.

XAML:

<DataGrid Name="g"
    AlternatingRowBackground="Blue"
    AlternationCount="2"
    ...
    SelectionMode="Single">
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding IsOrange}" Value="Y">
                    <Setter Property="Background" Value="Orange" />
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
    ...
</DataGrid>
解决方案

It's not a bug. In a Style you can't override a local value set for the alternating row. That's why this will not work

<DataGrid AlternatingRowBackground="Blue"

But if you set AlternatingRowBackground in a Style you can

<DataGrid.Style>
    <Style TargetType="DataGrid">
        <Setter Property="AlternatingRowBackground" Value="Blue"/>
    </Style>
</DataGrid.Style>

Thanks to this answer.

这篇关于WPF DataGrid AlternatingRowBackground和RowStyle优先级的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-06 00:49