locked
changing text colour with converter RRS feed

  • Question

  • Hi,

    I am getting an interesting issue.  I ahve a datagrid with template columns which are essentially textblocks.  The text in these textblocks have a both the fontweight and foreground set based on the data and I am using a converter to return an appropriate value:

    <data:DataGridTemplateColumn x:Name="dueDateColumn" Header="Response Due Date" Width="Auto"  CanUserSort="True" SortMemberPath="DueDate" >
                                <data:DataGridTemplateColumn.CellTemplate >
                                    <DataTemplate >
                                        <TextBlock Text="{Binding Path=DueDate, StringFormat=\{0:f\}}" FontWeight="{Binding Converter={StaticResource dueDateConverter}}" Foreground="{Binding Converter={StaticResource responseConverter}}" />
                                    </DataTemplate>
                                </data:DataGridTemplateColumn.CellTemplate>
                            </data:DataGridTemplateColumn>


    The one I am more concerned about is the foreground converter.  It gets the record and also looks at one of the associated tables.  Based on the logic it either returns red, yellow or black.

      public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
            {
               //1.check if any updates which are not description
                //if yes then return green colour
                //if no then check the time from now till due time.  If time is less than an hour return red otherwise return black
                Call _call = value as Call;
                SolidColorBrush _brush = new SolidColorBrush();
                if (_call.Updates.Count > 1)
                { 
                   //has more than one update indicates that a response has been sent
                    _brush = new SolidColorBrush(Colors.Green);
                }
                else
                { 
               //check timings
    
                    DateTime duedate = _call.DueDate;
                    DateTime datenow = DateTime.Now;
                    TimeSpan ts = duedate - datenow;
                    if (ts.Minutes <= 60)
                    {
                        _brush = new SolidColorBrush(Colors.Red);
                    }
                    else if (ts.Minutes <= 180)
                    {
                        _brush = new SolidColorBrush(Colors.Yellow);
                    }
                    else
                    {
                        _brush = new SolidColorBrush(Colors.Black);
                    }
                    
    
                }
    
                _brush.Color = new Color { A = (byte)(_brush.Color.A / 2), B = _brush.Color.B, G = _brush.Color.G, R = _brush.Color.R };
                return _brush;
            }


     

    What I am finding is when the datagrid fills up with data, all the text appears red. When I click on a record and see that it meets the criteria for the text to be green and then scroll down teh gatagrid so that the row is no longer in view, and then scroll back up again, the text has changed to green.  So it seems to be calling the converter only when a row is clicked - how to get it to call the converter when the row is being populated with data?

     

    Thanks in advance.

    Friday, August 20, 2010 6:45 AM

Answers

  • I found out the problem - my converter code was looking at a related table to see if there were any records.  In the initial data load I hadnt included the table hence the count always returned 0.  With the table included, the converter works fine and the colours are set on the initial data load.

     

    thanks 

    Tuesday, August 24, 2010 9:50 AM

All replies

  • Hi,

    The problem could be in the way, you are assigning the ItemsSource for the datagrid.

    How are you doing that?

    As I see that, when you scroll the row out of view, and back into view, the rows are redrawn or rendered again.

    A possible fix would be to call the UpdateLayout() for the datagrid, which forces to redraw/ render the datagrid contents, after assigning the data source. Something like,

    myDataGrid.UpdateLayout();


    Regards

    J

    Friday, August 20, 2010 4:40 PM
  • I found out the problem - my converter code was looking at a related table to see if there were any records.  In the initial data load I hadnt included the table hence the count always returned 0.  With the table included, the converter works fine and the colours are set on the initial data load.

     

    thanks 

    Tuesday, August 24, 2010 9:50 AM