User (Old forums)MemberFebruary 5, 2007 at 7:58 amPost count: 23064
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!
Imported from legacy forums. Posted by Thomas (had 3630 views)Xceed SupportMemberFebruary 5, 2007 at 11:24 amPost 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.
Row myRow = myDataGrid.ItemContainerGenerator.ContainerFromItem( myDataItem ) as Row;
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:
Cell myCell = myRow.Cells[ “columnName” ];
Note3: You should never keep a reference to a Cell neither.
Then you can:
myCell.Background = new SolidColorBrush( “HotPink” );
To return the the original background, you should use this method:
myCell.ClearValue( BackgroundProperty );
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)MemberFebruary 6, 2007 at 3:52 amPost 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.
Imported from legacy forums. Posted by Thomas (had 339 views)Xceed SupportMemberFebruary 6, 2007 at 8:27 amPost 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)MemberFebruary 6, 2007 at 9:42 amPost count: 23064
The problem was that the grid was not visible at this time. Now it works!
Imported from legacy forums. Posted by Thomas (had 4053 views)
- You must be logged in to reply to this topic.