locked
Change the Textblock foreground in datagrid: IValueconverter used...

    Question

  • I have a Date row, which I have created in datagrid. Please refer to the link/images below.

    Datagrid

    In the datagrid, I have used 2 Textblock in DataTemplete, one Textblock to show day(M,T,W and etc), and the second one to show date (03 May, 04 May and etc)
    In this Date row, user can forward or backward the date. And if the date is today date, the textblock color has to changed to blue. if it is not, then change back to black. You can seen as below.
    At first, I have used the CellStyle. If it is today date, then column[1] CellStyle = App.Current.Resource["Style1"] as style, and if column[1] after backward or foward, the date is not more today date, then column[1].CellStyle = App.Current.Resource["Style2"] as Style. However, using this method it returned error once trying to change to another CellStyle.

    Output

    I've searched in this forum and saw the method IValueConverter. This looks great and I have a tried. Below is my IValueConverter code.

     IValueConverter

    However, I've faced a great problem here. I really run out of idea at this place. I have tried 2 way of binding the IValueConverter.
    The first Method I tried is: Text = "{Binding TodayDate, Converter={StaticResource mc}}". However, once the result return,
    the text value from "02 May" becoming "#F00000". I know this is wrong, cause I didn't tell it should change the Foreground color. Therefore, I've changed to the second method.
    The second method is, Text = "{Binding TodayDate}" Foreground="{Converter={StaticResource mc}}". This one should be correct from the code. But the result return in IValueConverter as below print screen.


    The value has all the values...but how I can compare it one by one?
    As I only need to change the Textblock foreground to blue if that Textblock date is today date, or else, change to black.
    I got stuck at here and need help.
    I've spent one whole day to figure this out. :(
    Anyone please help!! Thank you!!


    I've stored the list of date value in a List<DateTime>.

    ItemSource 

    Wednesday, May 27, 2009 8:41 AM

Answers

  • It has been a while, I know, but I've just found your post.

     Shouldn't it be:

    Text = "{Binding TodayDate}" Foreground="{Binding Path=TodayDate, Converter={StaticResource mc}}"

    At least that's what works for me ...

    Tuesday, July 21, 2009 6:44 PM

All replies

  • It has been a while, I know, but I've just found your post.

     Shouldn't it be:

    Text = "{Binding TodayDate}" Foreground="{Binding Path=TodayDate, Converter={StaticResource mc}}"

    At least that's what works for me ...

    Tuesday, July 21, 2009 6:44 PM
  •  Hi Darek,

     Thanks for the reply. Smile
     I got it works in another way, but it is messy.
    Would try your suggested code!

    Once again, thanks alot!!

    Thursday, July 23, 2009 4:17 AM