Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
2.0k views
in Technique[技术] by (71.8m points)

wpf - How can I style a DataGridCell's content based on binding without naming that binding

I would like to create a style that makes my cell's content green if positive, red if negative or black if 0.

I know about converters and bindings, but is it possible to do this without naming the field the specific column is bound to (eg. I was to base on whatever is the cell's value)?

            <Style x:Key="GreenIfPositive" TargetType="DataGridCell" BasedOn="{StaticResource {x:Type DataGridCell}}">
                <Style.Triggers>
                    <DataTrigger Binding="{Binding, Converter={StaticResource greaterThanZeroDecimalConverter}}" Value="True">
                        <Setter Property="Foreground" Value="Green"/>
                    </DataTrigger>
                    <DataTrigger BBinding="{Binding, Converter={StaticResource greaterThanZeroDecimalConverter}}" Value="False">
                        <Setter Property="Foreground" Value="Red"/>
                    </DataTrigger>
                    <DataTrigger Binding="{Binding}" Value="0">
                        <Setter Property="Foreground" Value="Black"/>
                    </DataTrigger>
                </Style.Triggers>
            </Style>

So that I could use it on columns without re-iterating that style just so I can select the property I'm basing this on.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

here is a solution for DataGridTextColumns. DataGridTextColumn creates TextBlock element to display cell value. TextBlock has string Text property. Those TextBlocks can be accessed via DataGridCell Content property, so resulting binding path is "Content.Text"

<Style.Triggers>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, 
                 Path=Content.Text, Mode=OneWay,
                 Converter={StaticResource greaterThanZeroDecimalConverter}}" 
                 Value="True">
        <Setter Property="Foreground" Value="Green"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, 
                 Path=Content.Text, Mode=OneWay,
                 Converter={StaticResource greaterThanZeroDecimalConverter}}" 
                 Value="False">
        <Setter Property="Foreground" Value="Red"/>
    </DataTrigger>
    <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, 
                 Path=Content.Text, Mode=OneWay}" 
                 Value="0">
        <Setter Property="Foreground" Value="Black"/>
    </DataTrigger>
</Style.Triggers>

note {RelativeSource Self}.

I also had to change Convert method because Text is a string property and incoming value is string.

public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
    double d;
    if (value != null && value is string && double.TryParse(value.ToString(), out d))
    {
        return d > 0;
    }
    return null;
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...