This is a migrated thread and some comments may be shown as answers.

If Grid cell is a TextBlock, foreground is sometimes cleared

7 Answers 182 Views
GridView
This is a migrated thread and some comments may be shown as answers.
StackOverflowed
Top achievements
Rank 1
StackOverflowed asked on 15 Dec 2010, 07:15 PM
I sometimes hit strange behavior where if a column is brought out of view and then in again, the TextBlock's foreground property is set back to default Black. It looks like the offending code here is in GridViewCell.OnColumnChanged, which clears the TextBlock.Foreground property of the TextBlock in the cell (why it does this I don't understand).

I could derive from GridViewCell and put in my own behavior, since OnColumnChanged is protected, but the problem then becomes how to get the grid to use that cell instead of GridViewCell. I see that the only place GridViewCell is being created is in GridViewRow.CreateItem, which is an internal method and hence cannot be overriden by me. This leaves me no way of getting my CustomGridViewCell to be used in lieu of the default one.

Do you have any ideas of how I can deal with this issue?

Thanks

7 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 16 Dec 2010, 10:24 AM
Hi David,

Basically, the virtualization of the RadGridView is turned on by default. Consequently, the visual elements are re-used when scrolling vertically and horizontally, which may lead to a result similar to the one you experience. However, this behavior depends on the way you are setting the particular properties - the Foreground in your case. 
I am sending you the sample project I used for recreating your scenario. Please take a look at it and let me know in case there is some misunderstandings according to your requirements.

 
Regards,
Maya
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
StackOverflowed
Top achievements
Rank 1
answered on 20 Dec 2010, 06:14 PM
This seems to work fine because we're using a CellStyle to specify the Foreground.

However, if I specify a CellTemplate which has a TextBlock inside of it with its Foreground set, sometimes the grid will reset the Foreground, based on the code in GridViewCell. Is there a reason why a CellStyle is preferred vs. a local Foreground setter on the TextBlock itself?
0
Maya
Telerik team
answered on 20 Dec 2010, 06:40 PM
Hello David,

Basically, the CellStyle is defined on the GridViewCell level. Once you set a CellTemplate, whose element's properties contradict with those of the CellStyle (in your case Foreground property), the value set in the CellTemplate will be respected.
I have tested the issue you specified - that the CellStyle is sometimes applied and the Foreground of the TextBlock is not evaluated. However, I have not managed to reproduce the described behavior. Did you reproduce it in the sample project I attached previously ?
 

Best wishes,
Maya
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
StackOverflowed
Top achievements
Rank 1
answered on 20 Dec 2010, 08:07 PM
1) Change the GridViewDataColumn for the Name to the following:

<telerik:GridViewDataColumn DataMemberBinding="{Binding Name}">
    <telerik:GridViewDataColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding Name}" Foreground="HotPink" />
        </DataTemplate>
    </telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>

2) Start up the application
3) Position the mouse on the right border of the window so you see the horizontal resize grip ( looks like <--> )
4) Start resizing the window quickly, back and forth, so that you're occluding (i.e. hiding) the name column and showing it again.
5) Observe that not only the binding but the color of the cells changes.
0
Maya
Telerik team
answered on 21 Dec 2010, 09:32 AM
Hello David,

The behavior you are getting is due to the enabled-by-default virtualization, i.e. the visual elements are reused on scrolling/ resizing. However, we are in the process of improving this functionality so that you do not experience such a behavior.
For the time being I would definitely recommend you to use the approach of setting a style for the GridViewCell. Another possible way (in case you do not work with lots of columns) is to disable the horizontal virtualization (EnableColumnVirtualization = "False").  


Best wishes,
Maya
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
0
StackOverflowed
Top achievements
Rank 1
answered on 21 Dec 2010, 03:22 PM
I understand that the grid is virtualizing cells, which is desired behavior for me. The grid, however, is treating TextBlocks inconsistently with the way it treats any other control, as evidenced by the Foreground clearing that is being done in GridViewCell.

As a user I need to have a way to opt of out of this behavior. If I could supply my own GridViewCell which did not exhibit this behavior then that would be fine. But I can't because the GridViewRow method which creates GridViewCells is internal. Alternatively, if there was a switch to enable this behavior ONLY for TextBlocks but NOT for any class derived from TextBlock, that would help as well.

The point is that the grid is making an assumption that the GridViewCell Foreground clearing behavior is desirable for ALL TextBlocks, and that clearly is not the case here.

I hope a bug was opened for this issue.

Thanks
0
Maya
Telerik team
answered on 22 Dec 2010, 09:28 AM
Hi David,

Indeed, you are quite correct - the behavior you are getting is not the desired one. We do make effort in the direction of improving our virtualization and the fixes will be available in some of our next internal builds.  
For the time being I would recommend you to define a CellStyle for your columns. In case that does not meet your requirements, you may define your own custom column type as described in our online documentation and this blog post
Let me know if you need any further assistance.

Regards,
Maya
the Telerik team
Browse the videos here>> to help you get started with RadControls for WPF
Tags
GridView
Asked by
StackOverflowed
Top achievements
Rank 1
Answers by
Maya
Telerik team
StackOverflowed
Top achievements
Rank 1
Share this question
or