Powered by MSDN

US - English
NEW! Silverlight 5 is available Learn More

How to draw rectangle or image in column of datagrid RSS

12 replies

Last post Jan 15, 2012 07:55 PM by mtiede@swtechnologies.com

(1)
  • ggo

    ggo

    Member

    2 Points

    1 Post

    Re: Re: Re: Re: How to draw rectangle or image in column of datagrid

    Dec 28, 2010 05:22 PM | LINK

    Hi

    I am searching for a solution to change the background color of a specific cell of a row depending on the content. The DataGrid looks like this:

    Name | Last Online | State | E-Mail

    The Last Online Column shows a timestamp. If the user didnt logged in inbetween 13 days, the State column should have the color red, else green.

    On the search I found this code snippet from skea showing already the needed solutions - just with one problem:

    The object of the type System.Windows.Controls.DataGridCell cannot be casted into System.Windows.Controls.ContentPresenter

    Any suggestions?

    Thanks in advance,

    ggo

  • sajeetharan

    sajeetharan

    Member

    481 Points

    114 Posts

    Re: How to draw rectangle or image in column of datagrid

    Jan 15, 2012 12:01 PM | LINK

    @ggo!

    Replace the Line

    Customer c = ((Grid)((StackPanel)sender).Parent).DataContext as Customer;

    with this, it will work perfectly

      Customer c = ((DataGridCell)((StackPanel)sender).Parent).DataContext as Customer;

    Sajeetharan,
    Software Engineer,
    Skype id:sajee4ever
    http://www.sajeetharan.blogspot.com/
    Please mark the replies as answers if they help or unmark if not.
  • mtiede@swtechnologies.com

    mtiede@swtec...

    Contributor

    5366 Points

    2236 Posts

    Re: How to draw rectangle or image in column of datagrid

    Jan 15, 2012 07:55 PM | LINK

    ggo

    Hi

    I am searching for a solution to change the background color of a specific cell of a row depending on the content. The DataGrid looks like this:

    Name | Last Online | State | E-Mail

    The Last Online Column shows a timestamp. If the user didnt logged in inbetween 13 days, the State column should have the color red, else green.

    On the search I found this code snippet from skea showing already the needed solutions - just with one problem:

    The object of the type System.Windows.Controls.DataGridCell cannot be casted into System.Windows.Controls.ContentPresenter

    Any suggestions?

    Thanks in advance,

    ggo

    Just bind the background color in the template to a property in your datacontext.  Then add a valueconverterthat converts that timestamp to a solidcolorbrush.