wpf toolkit datagrid中的条件格式

时间:2022-10-26 22:19:02

hey i wanna change row foreground color according to a boolean in the model, whats the best way of doing it?

嘿我想根据模型中的布尔值更改行前景色,这是最好的方法吗?

2 个解决方案

#1


6  

Define the style as following (IsBlah is a boolian property):

将样式定义如下(IsBlah是boolian属性):

    <Style x:Key="MyRowStyle" TargetType="{x:Type dg:DataGridRow}">
        <Setter Property="Background" Value="White"/>
        <Setter Property="Foreground" Value="DarkBlue"/>            
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsBlah}" Value="False" >
                <Setter Property="Background" Value="DarkGray" />
                <Setter Property="Foreground" Value="White" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

Your DataGrid should have a custom RowStyle. (RowStyle="{StaticResource MyRowStyle})

您的DataGrid应该具有自定义RowStyle。 (RowStyle =“{StaticResource MyRowStyle})

#2


1  

This is basically the same answer as Boris, but here's the syntax if you prefer to define the style directly within the DataGrid definition.

这与Boris的答案基本相同,但如果您希望直接在DataGrid定义中定义样式,则可以使用此语法。

Note: Blend won't give you a live preview of this so you'll have to run it

注意:Blend不会为您提供实时预览,因此您必须运行它

<DataGrid>      
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasErrors}" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                </DataTrigger>      
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

#1


6  

Define the style as following (IsBlah is a boolian property):

将样式定义如下(IsBlah是boolian属性):

    <Style x:Key="MyRowStyle" TargetType="{x:Type dg:DataGridRow}">
        <Setter Property="Background" Value="White"/>
        <Setter Property="Foreground" Value="DarkBlue"/>            
        <Style.Triggers>
            <DataTrigger Binding="{Binding IsBlah}" Value="False" >
                <Setter Property="Background" Value="DarkGray" />
                <Setter Property="Foreground" Value="White" />
            </DataTrigger>
        </Style.Triggers>
    </Style>

Your DataGrid should have a custom RowStyle. (RowStyle="{StaticResource MyRowStyle})

您的DataGrid应该具有自定义RowStyle。 (RowStyle =“{StaticResource MyRowStyle})

#2


1  

This is basically the same answer as Boris, but here's the syntax if you prefer to define the style directly within the DataGrid definition.

这与Boris的答案基本相同,但如果您希望直接在DataGrid定义中定义样式,则可以使用此语法。

Note: Blend won't give you a live preview of this so you'll have to run it

注意:Blend不会为您提供实时预览,因此您必须运行它

<DataGrid>      
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding HasErrors}" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                </DataTrigger>      
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>