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

foreground color does not change in a custom header

3 Answers 145 Views
GridView
This is a migrated thread and some comments may be shown as answers.
BENN
Top achievements
Rank 1
BENN asked on 29 Feb 2016, 12:22 PM

I've followed the example at:

http://docs.telerik.com/devtools/wpf/controls/radgridview/troubleshooting/styling-custom-header.html

 

Well, it works for most cases, but it doesn't work if the column visibility is bound to a property, and it is invisible when the grid is loaded. It's hard for me to give a simple example that demonstrates exactly the scenario I have, but I've managed to find a different scenario, which reproduces the problem.

 

MyGridView.xaml

<UserControl x:Class="GridViewHeaderForground.MyGridView"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"
             xmlns:local="clr-namespace:GridViewHeaderForground"
             mc:Ignorable="d"
             d:DesignHeight="300" d:DesignWidth="300">
    <Grid>
        <telerik:RadGridView AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn>
                    <telerik:GridViewDataColumn.Header>
                        <TextBlock Text="First Column" Foreground="{Binding Foreground, RelativeSource={RelativeSource AncestorType={x:Type ContentControl}}}"/>
                    </telerik:GridViewDataColumn.Header>
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
        </telerik:RadGridView>
    </Grid>
</UserControl>

 

MainWindow.xaml

<Window x:Class="GridViewHeaderForground.MainWindow"
        xmlns:local="clr-namespace:GridViewHeaderForground"
        Title="MainWindow" Height="350" Width="525">
    <Grid>
        <TabControl SelectedIndex="0">
            <TabItem Header="1">
                <local:MyGridView />
            </TabItem>
            <TabItem Header="2">
                <local:MyGridView />
            </TabItem>
        </TabControl>
    </Grid>
</Window>

 

As you can see, the first tab looks fine, but if you switch to the second one, then the textblocks are all black.

If you try to "debug" it with snoop, and it fixes the binding problem.

 

My scenario is a little different: I have several RadPanes which only one of them is selected. On the other ones, few columns are invisible by using the IsVisible which is bound to a property.

When one of the RadPanes get selected, it loads (and so does the header cells). On the RowLoaded event, I get only the cells that are visible (if I'm correct).

Now if I change that property to true, the column becomes visible, but the textblock is black.

 

I think both problems are the same.

How do I solve it? I need to have both image and text in the header.

 

Thanks.

 

 

 

3 Answers, 1 is accepted

Sort by
0
BENN
Top achievements
Rank 1
answered on 01 Mar 2016, 07:21 AM

Hi, I've found an ugly solution.

Basically, the RowLoaded event is not good, since on the e.Row.Cells I'm only getting the visible cells.

On the other hand, I have the CellLoaded event, which happens before the RowLoaded event, and the e.Cell returns a cell which is not ready (I'm searching for a child of the TextBlock, which does not exist).

 

So, I have to wait for the cell to be ready, so I'm doing it with BeingInvoke:

radGrid.CellLoaded += new EventHandler<CellEventArgs>(radGrid_CellLoaded);

 

And then:

        void radGrid_CellLoaded(object sender, CellEventArgs e)
        {
            if (e.Cell is GridViewHeaderCell)
            {
                Action a = delegate()
                {
                    var textBlock = e.Cell.FindChildByType<TextBlock>();
 
                    if (textBlock != null)
                    {
                        var contentControl = Utils.FindParent<ContentControl>(textBlock);
                        Binding binding = new Binding("Foreground");
                        binding.Source = contentControl;
 
                        textBlock.SetBinding(TextBlock.ForegroundProperty, binding);
                    }
                };
                Dispatcher.BeginInvoke(a);
            }
        }
 
 
 
 
Where Utils.FindParent is:
 
        public static T FindParent<T>(DependencyObject depObj) where T : DependencyObject
        {
            DependencyObject parent;
            if (depObj != null)
            {
                parent = VisualTreeHelper.GetParent(depObj);
                while (parent != null)
                {
                    if (parent != null && parent is T)
                    {
                        return (T)parent;
                    }
                    parent = VisualTreeHelper.GetParent(parent);
                }
            }
            return null;
        }

 

It's ugly... but it's the only solution I've found so far.

Any other solution?

 

Thanks

 

0
Accepted
Stefan Nenchev
Telerik team
answered on 01 Mar 2016, 03:25 PM
Hello Benn,

Thank you for the detailed explanation provided.

I have further investigated the issue and can conclude that the behavior you are observing is normal. Actually, the workaround that you have shown in your second reply is exactly the same approach that we are using in case the Header is auto generated and the element within it is TextBlock. However, in the case of a custom header, we cannot be sure what the element within the Header would be so such binding is not automatically set. If you would like to further customize the Header when it is using the default TextBlock element, you can use the HeaderCellStyle and HeaderTextAlignment properties.

Please update me if the information provided was useful. Of course, if you have any further questions or concerns, do not hesitate to contact us.

Regards,
Stefan Nenchev
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
BENN
Top achievements
Rank 1
answered on 01 Mar 2016, 03:32 PM

Thank you for your reply.

Tags
GridView
Asked by
BENN
Top achievements
Rank 1
Answers by
BENN
Top achievements
Rank 1
Stefan Nenchev
Telerik team
Share this question
or