smoorthy
smoorthy

Reputation: 47

how to change the foreground of the datagrid textblock column using binding

In my application, I have used "DataGrid" to bind set of values dynamically. Also based upon the values binded, I need to change the forecolor of the values binded.

Here is my XAML and C# code

XAML:



                            <my:DataGridTemplateColumn Header="Priority">
                                <my:DataGridTemplateColumn.CellTemplate>
                                    <DataTemplate>
                                        <TextBlock HorizontalAlignment="Left" Name="txtPriority" Text="{Binding MessagePriority}" FontWeight="Bold">  
                                          <TextBlock.Resources>
                                            <Style TargetType="TextBlock">
                                                <Setter Property="Foreground" Value="{Binding ForegroundBrush}" />
                                            </Style>
                                        </TextBlock.Resources>
                                        </TextBlock>

                        </my:DataGrid.Columns>                            

                    </my:DataGrid>

C#:


private SolidColorBrush _foregroundBrush;

        public SolidColorBrush ForegroundBrush
        { 
            get
            {
                return _foregroundBrush;
            }
            set
            {
                if (_foregroundBrush != value)
                {
                    _foregroundBrush = value;
                    RaisePropertyChanged(() => _foregroundBrush);
                }
            }
        }

var color = (Color)ColorConverter.ConvertFromString("#FF00FF");
                                    var brush = new SolidColorBrush(color);
                                    ForegroundBrush = brush;  

Upvotes: 1

Views: 2607

Answers (2)

smoorthy
smoorthy

Reputation: 47

Hai Everyone,

        First of all i thanks to all for see my question and at that time send your answers. Now, i got the answer, i have to use the converter class for convert color to Solidcolorbursh like as below:

ValueConverter.cs

 public class ValueConverter : IValueConverter
    {
        public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value == null || !(value is decimal))
                return new SolidColorBrush(ForegroundBrush);

            var dValue = System.Convert.ToDecimal(value);
            if (dValue < 0)
                return new SolidColorBrush(ForegroundBrush);
            else
                return new SolidColorBrush(ForegroundBrush);
        }

        public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            return null;
        }

    }    

XMAL:

First we can decalre that class namespace like:

xmlns:convert="clr-namespace:Helper"
<Grid.Resources>
        <convert:ValueConverter x:Key="ValueConverter"></convert:ValueConverter>
 </Grid.Resources>

<my:DataGridTemplateColumn Header="Priority">
        <my:DataGridTemplateColumn.CellTemplate>
               <DataTemplate>
               <TextBlock HorizontalAlignment="Left" Name="txtPriority" Text="{Binding Priority}" FontWeight="DemiBold" Foreground="{Binding Priority, Converter={StaticResource ValueConverter}}">
 </TextBlock>

                  </DataTemplate>
            </my:DataGridTemplateColumn.CellTemplate>
</my:DataGridTemplateColumn>

Upvotes: 1

Sheridan
Sheridan

Reputation: 69985

There are a couple of ways that you can do this. It very much depends on your requirements, so it would have been better if you had explained them better and in more detail. However as you didn't, I can't use your exact details in this example and you will have to adapt it to your project. The first way is to simply use a DataTrigger... this method is good for up to around 8 different value/colour pairs:

<DataGrid ItemsSource="{Binding RadioButtonData}">
    <DataGrid.Columns>
        <DataGridTemplateColumn>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding Label}">
                        <TextBlock.Style>
                            <Style>
        <Style.Triggers>
            <DataTrigger Binding="{Binding SomeValue}" Value="Something">
                <Setter Property="TextElement.Foreground" Value="LightGreen" />
            </DataTrigger>
            <DataTrigger Binding="{Binding SomeValue}" Value="Another thing">
                <Setter Property="TextElement.Foreground" Value="LightBlue" />
            </DataTrigger>
            <DataTrigger Binding="{Binding SomeValue}" Value="A different thing">
                <Setter Property="TextElement.Foreground" Value="LightPink" />
            </DataTrigger>
        </Style.Triggers>
                            </Style>
                        </TextBlock.Style>
                    </TextBlock>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
    </DataGrid.Columns>
</DataGrid>

The other method that you can use is to use a Converter class with your Binding. You can find a detailed example of doing this in many online posts... here are a few:

How to set Foreground of DataGrid Column with ValueConverter
Datagrid AutoGenerateColumns="True" forecolor IValueConverter

Upvotes: 3

Related Questions