Click here to Skip to main content
15,890,512 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
I am new to WPF. I have a datagrid with a 15 columns of data. some of them are rations and i want to show the one with ratio less than 1 in green color font colour and the ones greater than 1 in Red.
is there any tutorials?

Cheers

What I have tried:

I tried to search and i found that the Developer Express can do that
Posted
Updated 8-May-16 14:29pm
Comments
Herman<T>.Instance 2-May-16 4:52am    
Create Styling XAML files

1 solution

Hi, just use style in Xaml. Refer below Xaml code, and style required columns of your datagrid.
XML
<DataGrid.Columns>
                <DataGridTextColumn Binding="{Binding YourProperty}" ClipboardContentBinding="{x:Null}" Header="Amount">
                    <DataGridTextColumn.CellStyle>
                        <Style TargetType="{x:Type DataGridCell}">
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding YourProperty, Converter={StaticResource NumberToBoolConverter}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="True">
                                    <Setter Property="Background" Value="Green" />
                                    <Setter Property="Foreground" Value="White" />
                                </DataTrigger>

                                <DataTrigger Binding="{Binding YourProperty, Converter={StaticResource NumberToBoolConverter}, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}" Value="False">
                                    <Setter Property="Background" Value="Red" />
                                    <Setter Property="Foreground" Value="White" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </DataGridTextColumn.CellStyle>
                </DataGridTextColumn>
            </DataGrid.Columns>

Use this converter,
C#
public class NumberToBooleanConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
    {
        try
        {
            bool ToReturn = false;
            int ActualValue = System.Convert.ToInt32(value);

            if (ActualValue <= 1)
            {
                ToReturn = true;
            }
            else if (ActualValue > 1)
            {
                ToReturn = false;
            }

            return ToReturn;
        }
        catch
        {
            return false;
        }
    }

    public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new InvalidOperationException("NumberToBooleanConverter can only be used OneWay.");
    }
}
 
Share this answer
 
v4
Comments
Member 12481067 2-May-16 7:32am    
sorry for my stupid question...
Does this code do green for x between 0 and 1 and red for value of x greater or equal than 1 ?
VR Karthikeyan 2-May-16 8:30am    
Solution updated, Check again.
Patrice T 8-May-16 9:44am    
You got a message as Solution 2
Member 12481067 8-May-16 10:03am    
I fixed it - thanks
Member 12481067 8-May-16 10:03am    
Hi
I made a very simple WPF application and loaded the datagrid with some numbers between 0 to 1.1, However i couldnt link the code you gave me to the application. it tells me : the resource "numbertobooleanconverter can not be resolved.
I can please have the project you have ? my email is resa.net@gmail.com
Cheers

I am really sorry i am not the sharpest tool in the shed when it comes to WPF.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900