繁体   English   中英

WPF DataGridTextColumn如何根据单元格的内容将前景色绑定为样式

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

我有一个DataGrid,其中我有以下内容

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

您可以看到我具有DataGridCell'ServiceStatusCell'的样式。 我想把

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

作为ServiceStatusCell样式的定义的一部分,但我还没有弄清楚该如何做。 大概我需要某种相对绑定来获取TextBlock的内容...但是经过大量实验,我无法做到这一点

这是ServiceStatusCell的定义:

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

任何帮助将不胜感激。

将设置器放在DataGridRow上对我来说很有效。 但是,这更改了整个行的前景

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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