简体   繁体   中英

WPF DataGridTextColumn how to bind the Foreground color in a style based on the content of the cell

I have a DataGrid where I have the following

<DataGridTextColumn Header="MyHeader"        
                    HeaderStyle="{StaticResource ServiceStatusColumn}"
                    ElementStyle="{StaticResource ServiceStatusElementStyle}"
                    Binding="{Binding PuServiceStatus, Converter={StaticResource serviceStatusText}}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell" BasedOn="{StaticResource ServiceStatusCell}">
            <Setter Property="Foreground" Value="{Binding PuServiceStatus, Converter={StaticResource serviceStatusColor}}" />
        </Style>
    </DataGridTextColumn.CellStyle>
 </DataGridTextColumn>

You can see that I have a style for the DataGridCell 'ServiceStatusCell'. I would like to put the

<Setter Property="Foreground" Value="{Binding PuServiceStatus, Converter={StaticResource serviceStatusColor}}" />

As part of the definition of the ServiceStatusCell style, but I have not figured out how to do so. Presumably, I need some type of relative binding that gets to the content of the TextBlock ... but after much experimentation, I have been unable to do so

Here is the definition of ServiceStatusCell :

<Style x:Key="ServiceStatusCell" TargetType="DataGridCell">
    <Setter Property="TextBlock.TextAlignment" Value="Center" />
    <Setter Property="Width" Value="20"/>
    <Setter Property="Height" Value="20"/>
</Style>

Any help would be greatly appreciated.

Putting the setter on the DataGridRow worked for me. This however changes the foreground for the entire row

<Style x:Key="RowProcessIdAndLevel" TargetType="{x:Type DataGridRow}" BasedOn="{StaticResource {x:Type DataGridRow}}">
        <Setter Property="Foreground" Value="{Binding Level, Converter={StaticResource LevelToForeground}}"/>
</Style>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM