代码之家  ›  专栏  ›  技术社区  ›  erotavlas

如何组合两个条件来覆盖datagrid的选定行样式?

  •  0
  • erotavlas  · 技术社区  · 7 年前

    我有一个DataGrid,集合中的对象有一个属性isvalid当一个项无效时(isvalid==false),我使用数据触发器将行前景颜色设置为红色。

    这是可行的,但是当我选择一个无效的行时,数据网格行上的选择样式会覆盖我设置的前景颜色,它不再是红色(它只是显示为默认的主题前景颜色)。

    我想把这个组合起来

    <Trigger Property="IsSelected" Value="true">
        <Setter Property="Foreground" Value="Red"></Setter>
    </Trigger>
    

    具备以下条件

    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow" BasedOn="{StaticResource MetroDataGridRow}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Path=isvalid}" Value="false">
                    <Setter Property="Foreground" Value="Red"></Setter>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
    

    一行的前景色应该是红色的,不管它是否被选中(但只有当isvalid==false时)

    我试着从 this post

    <MultiDataTrigger.Conditions>
        <Condition Property="IsSelected" Value="True"/>
        <Condition Binding="{Binding Path=isvalid}" Value="false" ></Condition>
    </MultiDataTrigger.Conditions>
    

    System.InvalidOperationException-必须具有非空值

    0 回复  |  直到 7 年前
        1
  •  3
  •   mm8    7 年前

    CellStyle 再加上一个 DataTrigger :

    <DataGrid ...>
        <DataGrid.CellStyle>
            <Style TargetType="{x:Type DataGridCell}">
                <Setter Property="Background" Value="Transparent"/>
                <Setter Property="BorderBrush" Value="Transparent"/>
                <Setter Property="BorderThickness" Value="1"/>
                <Setter Property="Template">
                    <Setter.Value>
                        <ControlTemplate TargetType="{x:Type DataGridCell}">
                            <Border BorderBrush="{TemplateBinding BorderBrush}" BorderThickness="{TemplateBinding BorderThickness}" Background="{TemplateBinding Background}" SnapsToDevicePixels="True">
                                <ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"/>
                            </Border>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
                <Style.Triggers>
                    <Trigger Property="IsSelected" Value="True">
                        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.HighlightTextBrushKey}}"/>
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.HighlightBrushKey}}"/>
                    </Trigger>
                    <Trigger Property="IsKeyboardFocusWithin" Value="True">
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static DataGrid.FocusBorderBrushKey}}"/>
                    </Trigger>
                    <MultiTrigger>
                        <MultiTrigger.Conditions>
                            <Condition Property="IsSelected" Value="true"/>
                            <Condition Property="Selector.IsSelectionActive" Value="false"/>
                        </MultiTrigger.Conditions>
                        <Setter Property="Background" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                        <Setter Property="BorderBrush" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightBrushKey}}"/>
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.InactiveSelectionHighlightTextBrushKey}}"/>
                    </MultiTrigger>
                    <Trigger Property="IsEnabled" Value="false">
                        <Setter Property="Foreground" Value="{DynamicResource {x:Static SystemColors.GrayTextBrushKey}}"/>
                    </Trigger>
                    <DataTrigger Binding="{Binding isvalid}" Value="false">
                        <Setter Property="Foreground" Value="Red"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.CellStyle>
        <DataGrid.RowStyle>
            <Style TargetType="DataGridRow" BasedOn="{StaticResource MetroDataGridRow}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding isvalid}" Value="false">
                        <Setter Property="Foreground" Value="Red"></Setter>
                    </DataTrigger>
                </Style.Triggers>
            </Style>
        </DataGrid.RowStyle>
    </DataGrid>
    
    推荐文章