View previous topic :: View next topic |
Author |
Message |
Eric
Joined: 06 Oct 2005 Posts: 15 Location: Regensburg, Germany
|
Posted: Tue Nov 08, 2005 7:55 pm Post subject: CellData Style: I want to specify a different color per cell |
|
|
Hi,
today, I would like to understand the CellData class.
A CellData is "a single cell (defined by a Row/Column) within a VirtualTree".
Why does the different styles property of a CellData refers to the rows and not only the current cell?
I may use the object CellData wrongly.
Here my piece of code where I'd like to specify the color of the cell. I am using object binding. Here, the Tree is a list of Node2nd, an object that has an attribut Color.
Code: | protected virtual void SubTree_GetCellValue(object sender, Infralution.Controls.VirtualTree.GetCellDataEventArgs e)
{
VirtualTree virtualtree = (VirtualTree)sender;
RowBinding rowbind = virtualtree.GetRowBinding(e.Row);
Node2nd node2 = (Node2nd)e.Row.Item;
if (node2 != null)
{
e.CellData.OddStyle.ForeColor = node2.Color;
e.CellData.EvenStyle.ForeColor = node2.Color;
}
// get the default binding for the given row and use it to populate the cell data
rowbind.GetCellData(e.Row, e.Column, e.CellData);
} |
The result of that is that all the rows of my tree have the color of the last Node2nd of the tree.
How can I specify a color per row? There is no Style property for a row (e.Row here), and the one of Rowbinding does not show anything in this case. |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Tue Nov 08, 2005 9:47 pm Post subject: |
|
|
If you are using Object binding then you probably should set the RowBinding and CellBinding styles using the VirtualTree design time editor. See the following thread for more information on how to set the color for an entire row:
http://www.infralution.com/phpBB2/viewtopic.php?t=160
The reason your code doesn't work is that VirtualTree does not create an individual style object (with colors, fonts etc) for each row of the tree. By default the style objects referenced in the CellData are the VirtualTree.RowOddStyle and RowEvenStyle. You could create a new style, if you wanted, in the GetCellData method and set this as the style - but if you just change the ForeColor of the style as you have then you will change the color for all rows which use this style - which by default is all rows.
It is better (for performance) not to create a new style in GetCellData but to create a member variable which holds the style and then you set this in GetCellData. This is exactly what the RowBindings and CellBindings do - so you are best off using them instead unless you really need something more dynamic. _________________ Infralution Support |
|
Back to top |
|
|
Eric
Joined: 06 Oct 2005 Posts: 15 Location: Regensburg, Germany
|
Posted: Mon Nov 14, 2005 6:23 pm Post subject: |
|
|
I am back in this topic, because I still do not see the solution (I was busy with other points until now).
So, as I said, I am already working in the GetCellData.
Concretly, what do you mean with "create a member variable which holds the style and then you set this in GetCellData" ?
If I add an attribut to my class:
Code: | private Color _Color; |
, set rowbing or cell binding Style, or OddStyle/EvenStyle in the VirtualTree_InitializeControl event:
Code: | rowbind.Style.ForeColor = _Color;
cellbind.Style.ForeColor = _Color; |
and then set the attribut in GetCellData:
Code: | _Color = node2.Color; | , I do not have any color !
As said, rowbinding and cellbinding Styles seems not to work in my case. |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Mon Nov 14, 2005 9:29 pm Post subject: |
|
|
You can't just set the color property of the style - this will set the color of all rows that use this style (which is every row in the tree). If you want a row to have a different color then you need to assign in a new (or different) Style. For example in GetCellData and GetRowData you could do:
Code: |
Style highlightStyle = new Style(_virtualTree.RowStyle);
highlightStyle.Color = Color.Red;
cellData.EvenStyle = highlightStyle;
cellData.OddStyle = highlightStyle;
|
However it would be better for performance to create the highlight style in your constructor and just do the assignments in GetCellData/GetRowData. _________________ Infralution Support |
|
Back to top |
|
|
Eric
Joined: 06 Oct 2005 Posts: 15 Location: Regensburg, Germany
|
Posted: Tue Nov 15, 2005 8:29 am Post subject: |
|
|
Ok, with your piece of code, it is working as I need (with highlightStyle.ForeColor )
I've tried the better version with new Style() in my constructor or in my VirtualTree_InitializeControl event, but it does almost the same as without this new style: all the rows are using the same color. The GetCellData is called many times and what is shown is only from the last occurence. |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Tue Nov 15, 2005 10:14 am Post subject: |
|
|
For the "better" code to work you would have to set the color in the constructor as well as creating the style. You would only set those rows you wanted to be the different color to then use this style (others would use the default style). _________________ Infralution Support |
|
Back to top |
|
|
|