conditional formatting in wpf toolkit datagrid

左心房为你撑大大i 提交于 2020-01-06 03:18:09

问题


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


回答1:


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

    <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})




回答2:


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.

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

<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>


来源:https://stackoverflow.com/questions/1113822/conditional-formatting-in-wpf-toolkit-datagrid

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!