Home Forums WPF controls Xceed DataGrid for WPF Individual Coloring of cells

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • User (Old forums)
    Member
    Post count: 23064
    #21673 |

    Hi

    I’m searching of a sample how I could color one cell in a grid.
    I’m using a ObservableCollection as Itemsource and my aim is to color one cell by using C# code.

    Thanks for your help!

    Thomas

    Imported from legacy forums. Posted by Thomas (had 3630 views)

    Xceed Support
    Member
    Post count: 5658

    The easiest way to Color a cell using C# code is to ask the ItemContainerGenerator for the Row that contains the specific cell you need to color. There are however some limitations you need to be aware of.

    <code>
    Row myRow = myDataGrid.ItemContainerGenerator.ContainerFromItem( myDataItem ) as Row;
    </code>

    Note: the value returned by this function CAN be null ( that means that the Row is “virtualized” ).

    Note2: You should never keep a reference to a Row obtained through this method as the Row can be “re-virtualized”.

    Then you can access the Cell by specifying:

    <code>
    Cell myCell = myRow.Cells[ “columnName” ];
    </code>

    Note3: You should never keep a reference to a Cell neither.

    Then you can:

    <code>
    myCell.Background = new SolidColorBrush( “HotPink” );
    </code>

    To return the the original background, you should use this method:

    <code>
    myCell.ClearValue( BackgroundProperty );
    </code>

    To ensure that the styles/triggers are re-evaluated for the cell.

    If the limitation of passing through the ItemContainerGenerator and/or having the possibility that the container returned is null are bothering you, I can suggest looking into creating an Implicit Style for DataCell and placing a trigger in it for the Background color.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 475 views)

    User (Old forums)
    Member
    Post count: 23064

    Sorry but this does not really work. The containerFromItem method returns always null. So I use a observable list as item source. I tried also to use the ContainerFromIndex(1) method but it returns null too. My datagrid contains 20 rows.

    Whats wrong?

    Imported from legacy forums. Posted by Thomas (had 339 views)

    Xceed Support
    Member
    Post count: 5658

    Are the 20 rows visible when you try to set the Cell color?

    If not, it is possible that the row you try to access is “virtualized” (has no UI yet).

    However, if the row/cell you try to color is visible when you try to do it, then I’d like to see how your are doing it and when you are doing it.

    Imported from legacy forums. Posted by Marcus [Xceed] (had 397 views)

    User (Old forums)
    Member
    Post count: 23064

    The problem was that the grid was not visible at this time. Now it works!

    Thanks!

    Imported from legacy forums. Posted by Thomas (had 4053 views)

    User (Old forums)
    Member
    Post count: 23064

    hello

    i’ve put the code but then i lose Error Cell’s and RowSelector’s configuration,
    how can i avoid this?
    and that only background’s colour changes

     

    Imported from legacy forums. Posted by J (had 1043 views)

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.