Roberto Salemi

Roberto Salemi

  • NA
  • 146
  • 136.8k

[WPF] Use IValueConverter but hide the value in cell

Jan 22 2015 9:34 AM
Hi, 
I have a column with a range of age.

I'm using a Convert to set a background color to a single cell of my column 'age'...
I would color the cell but hide the numeric value.

I can set a custom background, but I can't hide the value.

How can I make this?


This is my code:
XAML:
                    <DataGridTextColumn Binding="{Binding Age}" Header="Age - Color Range" Width="200">
                        <!-- COLOR CELLA -->
                        <DataGridTextColumn.ElementStyle>
                            <Style TargetType="{x:Type TextBlock}">
                                <Setter Property="Background" Value="{Binding Age, Converter={StaticResource ColorToCell}}"/>
                            </Style>
                        </DataGridTextColumn.ElementStyle>
                    </DataGridTextColumn>

C#
public class ColorToCell : IValueConverter
    {

        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            int tempValue = int.Parse(value.ToString());
            string tempString = "Red";
            if (tempValue >= 0 && tempValue <= 20)
                tempString = "#FF0000";

            if (tempValue > 20 && tempValue <= 40)
                tempString = "#F09300";

            if (tempValue > 40 && tempValue <= 60)
                tempString = "#EDDF00";

            if (tempValue > 60 && tempValue <= 80)
                tempString = "#CC00FF55";

            if (tempValue > 80 && tempValue <= 100)
                tempString = "#85AB00";


            SolidColorBrush brush = new SolidColorBrush();
            BrushConverter conv = new BrushConverter();
            brush = conv.ConvertFromString(tempString) as SolidColorBrush;
            return brush;

        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {                        
            return DependencyProperty.UnsetValue;
        }
    }

Thanks.

Answers (5)