View previous topic :: View next topic |
Author |
Message |
jkempner
Joined: 03 Sep 2008 Posts: 17
|
Posted: Fri Mar 04, 2011 7:29 pm Post subject: Style row when cell editor changes a value |
|
|
I'm struggling with a row style issue... I have a control derived from VirtualTree with a CheckBox cell editor defined in one column. I'd like to be able to set the style of a row, such as make the text bold or change the BackColor, based on the state of the check box in that row. I was thinking of something like this:
Code: |
private void cellEditorCheckBox_SetControlValue(object sender, CellEditorSetValueEventArgs e)
{
CheckBox checkBox = (CheckBox)e.Control;
if (checkBox.Checked)
{
// Set row background color and font
}
else
{
// Revert row background color and font to default
}
}
|
I can't seem to get this to work on a single row, only on odd/even rows or all rows in the whole control. Is this doable? Am I going about it completely the wrong way? |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Sat Mar 05, 2011 1:44 am Post subject: |
|
|
Generally when you want to make changes in appearance that are conditional like this you should think of them as reflecting some change/difference in the underlying data ie you choose the style to display the row with based on the underlying data. If you use this data driven approach (which Virtual Tree is based on) then your data will always display correctly and won't be dependent on whether the change was made by a user or was made externally.
In this case presumably the CheckBox value is mapped to some property in the bound data object. If it isn't then you may need to programmatically bind it to a data collection of some kind which you can the query to determine if a given row is checked. The File Browser sample code does this to manage the which directories are checked in the left hand tree.
So now you need to make the row style dependent on the same property (or data collection). To do this you will need to override the OnGetRowData and OnGetCellData methods and select the style to use based on the checked property/collection. Note that you can't just set the Font or Color of the styles in the CellData/RowData objects - as this will affect all rows that use that style. Instead you either need to create a new style (with the parent style) each time OnGetRowData\OnGetCellData is called - or more efficiently - you create the "checked" style upfront and then just set the CellData.OddStyle and EvenStyle properties to the "checked" style. See the following FAQ for more discussion on dynamically setting styles:
http://www.infralution.com/phpBB2/viewtopic.php?t=419 _________________ Infralution Support |
|
Back to top |
|
|
jkempner
Joined: 03 Sep 2008 Posts: 17
|
Posted: Mon Mar 07, 2011 10:27 pm Post subject: |
|
|
Thanks for the help. Along the lines of that suggestion, I added some code to my existing override of OnGetRowData to modify RowData.EvenStyle and RowData.OddStyle for the rows that are part of the "checked" collection, but it doesn't seem to have any effect on how the table is drawn. What I've done is this (with some class member declarations stripped out):
Code: | public TreeControl()
{
StyleDelta delta = new StyleDelta();
delta.Font = new Font(this.RowStyle.Font, FontStyle.Bold);
boldStyle = new Style(this.RowStyle, delta);
}
protected override bool SetValueForCell(Row row, Column column, object oldValue, object newValue)
{
if (column == this.checkColumn)
{
checkedItems.Add(row.Item);
}
}
protected override void OnGetRowData(Row row, RowData rowData)
{
if (checkedItems.Contains(row.Item))
{
rowData.EvenStyle = boldStyle;
rowData.OddStyle = boldStyle;
}
}
|
Is there something else I need to do to get the control to draw the checked rows using the styles that I set in OnGetRowData? |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Tue Mar 08, 2011 6:48 am Post subject: |
|
|
Quote: | Is there something else I need to do to get the control to draw the checked rows using the styles that I set in OnGetRowData |
Yes - you need to notify Virtual Tree that the data has changed. You can force Virtual Tree to requery the data for a row by calling UpdateRowData eg
Code: | protected override bool SetValueForCell(Row row, Column column, object oldValue, object newValue)
{
if (column == this.checkColumn)
{
checkedItems.Add(row.Item);
// shouldn't you add the item if newValue is true otherwise remove
// the item?
}
row.Tree.UpdateRowData(row);
} |
_________________ Infralution Support |
|
Back to top |
|
|
jkempner
Joined: 03 Sep 2008 Posts: 17
|
Posted: Tue Mar 08, 2011 6:07 pm Post subject: |
|
|
Adding a call to row.Tree.UpdateRowData(row) at the end of the SetValueForCell doesn't seem to help. SetValueForCell is getting called before OnGetRowData, so I can see that the underlying data have been updated by the time OnGetRowData is called and can confirm that the EvenStyle and OddStyle properties are getting set correctly. However, the rows whose styles have been set in OnGetRowData are still getting drawn using the default RowStyle.
BTW, the actual code does add/remove the item to/from the checkedItems list depending on the value of newValue. I had stripped out a bunch of irrelevant code after pasting it into my post, and apparently I stripped out too much. |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Tue Mar 08, 2011 10:22 pm Post subject: |
|
|
Sorry - I see the issue now. You are only setting the style for the Row. You need to override OnGetCellData and set the style for the cells. _________________ Infralution Support |
|
Back to top |
|
|
jkempner
Joined: 03 Sep 2008 Posts: 17
|
Posted: Wed Mar 09, 2011 4:22 pm Post subject: |
|
|
Setting the style for the cells instead of the row does the trick. The row style seems to be irrelevant, as it works with or without setting the row style as long as I set the style in OnGetCellData. It seems a bit odd to me from a design perspective that setting the row's style does nothing, while setting the cells' style using the identical Style object changes the way the whole row is drawn - is this a bug or a feature? |
|
Back to top |
|
|
Infralution
Joined: 28 Feb 2005 Posts: 5027
|
Posted: Wed Mar 09, 2011 9:07 pm Post subject: |
|
|
It's because of the way style "inheritance" (this is parenting not C# inheritance) works. We had to make a choice between the default cell style inheriting from the column cell style or the individual row style. We made the choice that the more common case is changing the style of a column and so the cell style inherits the column cell style (which in turn inherits from the general row style). This means that changing the individual row style only affects those areas that are outside of the cells. _________________ Infralution Support |
|
Back to top |
|
|
|