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

Custom HeaderCellStyle problem (Q3 Beta 2)

6 Answers 164 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Marjeta Segrec
Top achievements
Rank 1
Marjeta Segrec asked on 24 Oct 2009, 08:24 AM
Hello.

With the latest build Beta2_2009_3_1023 (with build  Beta2_2009_3_1019 works OK) custom HeaderCellStyle (MyHeaderCellStyle2) cause error :

<Style x:Key="MyHeaderCellStyle2" TargetType="controls:GridViewHeaderCell">  
            <Setter Property='Template'>  
                <Setter.Value> 
                    <ControlTemplate TargetType="controls:GridViewHeaderCell">  
                        <Grid Margin='2'>  
                            <Grid.ColumnDefinitions> 
                                <ColumnDefinition Width='*' /> 
                                <ColumnDefinition Width='Auto' /> 
                            </Grid.ColumnDefinitions> 
                            <TextBlock Text='Sample' VerticalAlignment='Center' /> 
                        </Grid> 
                    </ControlTemplate> 
                </Setter.Value> 
            </Setter> 
        </Style> 

Error: Unhandled Error in Silverlight Application
Code: 4004   
Category: ManagedRuntimeError      
Message: System.NullReferenceException: Object reference not set to an instance of an object.
   at Telerik.Windows.Controls.GridView.GridViewHeaderCell.OnApplyTemplate()
   at System.Windows.FrameworkElement.OnApplyTemplate(IntPtr nativeTarget)

If you do not Set TemplateProperty than works :
<Style x:Key="MyHeaderCellStyle1" TargetType="controls:GridViewHeaderCell">  
            <Setter Property="Foreground" Value="Red" /> 
            <Setter Property="FontSize" Value="20" /> 
            <Setter Property="FontWeight" Value="Bold" /> 
            <Setter Property="FontStyle" Value="Italic" /> 
        </Style> 

Regards,

6 Answers, 1 is accepted

Sort by
0
Missing User
answered on 25 Oct 2009, 02:44 PM
Hello Marjeta Segrec,

Unfortunately you are right, we were able to reproduce the problem with our latest internal build due to your detailed description. We have logged out the problem in our bug tracking system and we will do our best to fix it for our next official release (scheduled for early November).
As a workaround I can suggest you using the Header property (with our Q2 2009 we introduced new column property Header (of type object) similar to MS DataGrid . Generally with this new property you can build any kind of UI directly in XAML without styles.) You can put any kind of content in the Header property. Here is how you achieve this:

<telerik:RadGridView x:Name="RadGridView1"   
                     AutoGenerateColumns="False">
            <telerik:RadGridView.Columns>
                <telerik:GridViewDataColumn UniqueName="Name" >
                    <telerik:GridViewDataColumn.Header >
                            <TextBlock Text='Sample Text Here' VerticalAlignment='Center' />
                    </telerik:GridViewDataColumn.Header>
                </telerik:GridViewDataColumn>
            </telerik:RadGridView.Columns>
</telerik:RadGridView>

I have updated your Telerik points for you involvement.

All the best,
Anastasia
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Tim
Top achievements
Rank 1
answered on 02 Nov 2009, 08:28 PM
Hi, Anastasia.  I was having this same problem.  Your suggestion to use the Header property did resolve the error, but I am now having other quirky behavior with the grid...

Initially, I was creating my own HeadCellStyle so that I could Wrap the header text:

        <Style x:Key="tGridViewHeaderStyle" TargetType="tGridView:GridViewHeaderCell">  
            <Setter Property="Template" > 
                <Setter.Value> 
                    <ControlTemplate  TargetType="tGridView:GridViewHeaderCell">  
                        <tGridView:AlignmentContentPresenter TextWrapping="Wrap" TextAlignment="Center" VerticalAlignment="Center" /> 
                    </ControlTemplate> 
                </Setter.Value> 
            </Setter> 
        </Style> 
 

Of course, the one major problem with this is that it was overriding the built-in filtering icon and functionality for the columns.  I was about to ask how to add that back when I upgraded to the Q3 Beta 2 which caused the original error reported by Marjeta.  Using your Header property work around stopped the error and even left the built-in filtering stuff intact.  I was pleased.  But after only a few minutes of playing around, I noticed that none of the column freezing was working anymore, and any horizontal scrolling was doing bizarre things like resizing the column headers and such... seeminly at random.

So, basically, I went from using the cutsom style above with something like this (which had no errors in the previous version but does now, and I lost the built-in filtering):

    <tControls:RadGridView Margin="0,4,0,0" x:Name="radGridView" FontFamily="Arial" BorderThickness="1" Background="Transparent" 
                           AutoGenerateColumns="False" CanUserInsertRows="False" ShowGroupPanel="False" ShowGroupFooters="False" ShowColumnFooters="True" 
                           GridLinesVisibility="Both" Height="auto" CanUserFreezeColumns="False" CanUserReorderColumns="False" 
                           FrozenColumnCount="3" 
                        > 
        <tControls:RadGridView.Columns> 
            <tControls:GridViewDataColumn Header="Project ID" DataMemberBinding="{Binding ProjectID}" IsReadOnly="True" Width="75" HeaderCellStyle="{StaticResource tGridViewHeaderStyle}"></tControls:GridViewDataColumn> 
        </tControls:RadGridView.Columns> 
    </tControls:RadGridView> 
 

Now I'm doing something like this (which leaves the built-in filtering, but doesn't allow my frozen colums to work and causes strange scrolling behavior):

    <tControls:RadGridView Margin="0,4,0,0" x:Name="radGridView" FontFamily="Arial" BorderThickness="1" Background="Transparent" 
                           AutoGenerateColumns="False" CanUserInsertRows="False" ShowGroupPanel="False" ShowGroupFooters="False" ShowColumnFooters="True" 
                           GridLinesVisibility="Both" Height="auto" CanUserFreezeColumns="False" CanUserReorderColumns="False" 
                           FrozenColumnCount="3" 
                        > 
        <tControls:RadGridView.Columns> 
            <tControls:GridViewDataColumn DataMemberBinding="{Binding ProjectID}" IsReadOnly="True" Width="75" > 
                <tControls:GridViewDataColumn.Header> 
                    <TextBlock Text="Project ID" TextWrapping="Wrap" TextAlignment="Center" VerticalAlignment="Center"></TextBlock> 
                </tControls:GridViewDataColumn.Header> 
            </tControls:GridViewDataColumn> 
        </tControls:RadGridView.Columns> 
    </tControls:RadGridView> 
 

Naturally, I have many more columns than that, but I left them out for brevity.

So, I'm looking to get my frozen columns working properly again and have the built-in filtering (which I lose when applying a new style).  Any help would be very appreciated!

Thanks!
0
Missing User
answered on 03 Nov 2009, 04:54 PM
Hello Tim,

With your help we have identified a bug. Unfortunately we won't be able to fix it for our official Q3 release, but the fix will be included in our first service pack.
However the problem can be workaround by setting AutoGenerateColumns="False" after you set CanUserFreezeColumns="False" and  FrozenColumnCount="3" like this:
<telerik:RadGridView Name="gridView" CanUserFreezeColumns="False" FrozenColumnCount="3" AutoGenerateColumns="False"/>

Regarding the 'resizing column headers' problem which appear when you scroll the grid horizontally, you can set a fixed Height of the HeaderRow by a style:

<Style TargetType="gridView:GridViewHeaderRow" x:Key="HeaderRowStyle">
       <Setter Property="Height" Value="50"/>
</Style>

I'm attaching a sample application for your reference.

P.S. I have updated your Telerik points for you involvement.

Best wishes,
Anastasia
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Tim
Top achievements
Rank 1
answered on 10 Nov 2009, 09:57 PM
Hi, Anastasia.  Sorry it took me a week to get back to you.  Your workaround with setting the height for the HeaderRow worked fine.  My only remaining issue has to do with a very narrow dummy column that I was using to simulate the frozen column bar (I don't want the users to be able to move the frozen column bar, but when I disable their ability to do that, the bar goes away, and it makes it difficult to tell where the frozen column line is).  So, I had created a column with a width=3 and a different background color.  However, when I was forced to go away from my original code of setting the HeaderCellStyle to creating a TextBlock inside the Header object, the dummy column is appearing much wider, and I can't seem to force it to be narrower regardless of what I set the width to.  Could this be because the TextBlock object that is now inside the Header object is forcing that column to be wider?

Maybe I'm making this more difficult than it has to be.  Is there a better way of creating a pseudo-frozen-column-bar that the user cannot move?

If it would be easier for me to send you the whole solution for examination, please let me know and I'd be more than happy to post it.  Otherwise, here is the column definition in question (I have tried it with both a TextBlock and the AlignmentContentPresenter inside the Header, both with the same results of being at least 20 pixels wide or so when I'm looking for something much thinner).

            <tControls:GridViewDataColumn UniqueName="ViewID" Background="LightGray" DataMemberBinding="{Binding ViewID}" IsResizable="False" IsReadOnly="True" Width="3">  
                <tControls:GridViewDataColumn.Header> 
                    <tGridView:AlignmentContentPresenter TextWrapping="Wrap" Content=""></tGridView:AlignmentContentPresenter> 
                </tControls:GridViewDataColumn.Header> 
            </tControls:GridViewDataColumn> 
 

Thanks in advance for any advice.

-Tim
0
Kalin Milanov
Telerik team
answered on 16 Nov 2009, 10:42 AM
Hi Tim,

Could you please try the following column definition and tell me if this fits your case:
<telerik:GridViewColumn MaxWidth="3" Background="Red" IsReadOnly="True" IsReorderable="False" IsFilterable="False" IsSortable="False" />
Of course you can change the background as to match your design. Also I have placed this column as the fourth one and set FrozenColumnCount="4" to accommodate the change.

Let me know if this suits your case.

Regards,
Kalin Milanov
the Telerik team

Instantly find answers to your questions on the new Telerik Support Portal.
Watch a video on how to optimize your support resource searches and check out more tips on the blogs.
0
Tim
Top achievements
Rank 1
answered on 16 Nov 2009, 08:32 PM
Kalin,
    Using MaxWidth="3" instead of Width="3" did the trick.  Thanks.
Tags
GridView
Asked by
Marjeta Segrec
Top achievements
Rank 1
Answers by
Missing User
Tim
Top achievements
Rank 1
Kalin Milanov
Telerik team
Share this question
or