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

Header click event exception

5 Answers 96 Views
GridView
This is a migrated thread and some comments may be shown as answers.
This question is locked. New answers and comments are not allowed.
Jon Barron
Top achievements
Rank 1
Jon Barron asked on 09 Aug 2010, 11:04 PM

Hi,

I'm getting a strange exception when I create a column that has a header with the MouseLeftButtonEvent bound.

I create the header as follows:

 

 

GridViewHeaderCell header = new GridViewHeaderCell {Content = column.Name, FilteringUIVisibility = Visibility.Collapsed};

 

header.MouseLeftButtonUp += SSSGridViewColumn_MouseLeftButtonUp;

Header = header;



Now when I click the header, the method bound to the event fires, but the Silverlight app throws a Null reference exception after my method returns:

Stack:
+  [System.NullReferenceException] {System.NullReferenceException: Object reference not set to an instance of an object.
   at Telerik.Windows.Controls.GridView.GridViewHeaderCell.OnMouseLeftButtonUp(MouseButtonEventArgs e)
   at System.Windows.Controls.Control.OnMouseLeftButtonUp(Control ctrl, EventArgs e)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)} System.NullReferenceException

5 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 10 Aug 2010, 06:46 AM
Hi Jon Barron,

 Can you verify using the debugger if the exception is not inside this event handler?

Kind regards,
Vlad
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Jon Barron
Top achievements
Rank 1
answered on 10 Aug 2010, 03:20 PM
I have verified that the exception is not inside the event handler by commenting out all of the code inside the handler; the exception is still thrown. Also, if the exception were in the event handler, that's where the exception breakpoint would've hit, not from an [External Code] reference.

I put together a small project to isolate the behavior, and it's very easy to reproduce. Simply bind the grid to a collection, add a custom column, and then bind to the event. After the event fires the exception will be raised. I can email you the project if you wish (it won't let me attach a zip file to this).

XAML:

<

 

 

UserControl x:Class="BugReport1.MainPage"

 

 

 

xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

 

 

 

xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"

 

 

 

xmlns:d="http://schemas.microsoft.com/expression/blend/2008"

 

 

 

xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"

 

 

 

mc:Ignorable="d"

 

 

 

d:DesignHeight="300" d:DesignWidth="400" xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation" xmlns:BugReport1="clr-namespace:BugReport1">

 

 

 

 

<UserControl.Resources>

 

 

 

 

<BugReport1:TestTable x:Key="TestTable"/>

 

 

 

 

</UserControl.Resources>

 

 

 

 

<Grid x:Name="LayoutRoot" Background="White">

 

 

 

 

<telerik:RadGridView Name="grdSpreadsheet" SelectedItem="{Binding Path=SelectedRow, Mode=TwoWay}"

 

 

 

ItemsSource="{StaticResource TestTable}"

 

 

 

ShowGroupPanel="False" Grid.Row="0" AutoGenerateColumns="False" AlternateRowBackground="#FF97CFDE"

 

 

 

CanUserSortColumns="False" CanUserSelect="True" IsFilteringAllowed="False" />

 

 

 

 

</Grid>

 

</

 

 

UserControl>

 


Code behind:

using

 

 

System.Windows.Controls;

 

using

 

 

System.Windows.Data;

 

using

 

 

System.Windows.Input;

 

using

 

 

Telerik.Windows.Controls;

 

using

 

 

Telerik.Windows.Controls.GridView;

 

namespace

 

 

BugReport1

 

{

 

 

public partial class MainPage : UserControl

 

{

 

 

public MainPage()

 

{

InitializeComponent();

 

 

GridViewDataColumn column = new GridViewDataColumn();

 

 

 

GridViewHeaderCell header = new GridViewHeaderCell { Content = "Name" };

 

header.MouseLeftButtonUp += SSSGridViewColumn_MouseLeftButtonUp;

column.Header = header;

column.DataMemberBinding =

 

new Binding("Name");

 

grdSpreadsheet.Columns.Add(column);

}

 

 

static void SSSGridViewColumn_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

 

{

 

 

int dosomething = 0;

 

dosomething++;

 

// this line is hit prior to the exception

 

e.Handled =

 

false; // this line makes no difference -- true/false/removed

 

 

 

// exception is thrown somewhere after this point.

 

}

}

}


Test data structure: (TestTable.cs)

using

 

 

System.Collections.Generic;

 

namespace

 

 

BugReport1

 

{

 

 

public class TestTable : List<Column>

 

{

 

 

public TestTable()

 

{

Add(

 

new Column("A"));

 

Add(

 

new Column("B"));

 

Add(

 

new Column("C"));

 

Add(

 

new Column("D"));

 

Add(

 

new Column("E"));

 

}

}

 

 

public class Column

 

{

 

 

public Column()

 

{

}

 

 

public Column(string name)

 

{

Name = name;

}

 

 

public string Name { get; set; }

 

}

}




0
Vlad
Telerik team
answered on 10 Aug 2010, 04:03 PM
Hi,

 I'm afraid that you cannot add a header cell (GridViewHeaderCell) like this. For example currently this newly created header cell is not associated with any column (nor grid). Can you post more info about your scenario? For example you can add any UI element for the column Header property and use this UI element events.

All the best,
Vlad
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
0
Jon Barron
Top achievements
Rank 1
answered on 10 Aug 2010, 04:33 PM
What is the proper way to add a custom header control to a column?

It (seemed) proper as I have done to declare a column, then declare a header (GridViewHeaderCell), and then set that column's Header property to reference the cell.

I simply want to be able to bind to the click event of a column header so I can give the user a surface to select a column (not sort, but simply indicate they have "selected" the column)
0
Vlad
Telerik team
answered on 11 Aug 2010, 06:38 AM
Hello,

 Generally the grid should generate GridViewHeaderCell - if you want custom control in the column header you can use column Header property. 

Kind regards,
Vlad
the Telerik team
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 Public Issue Tracking system and vote to affect the priority of the items
Tags
GridView
Asked by
Jon Barron
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Jon Barron
Top achievements
Rank 1
Share this question
or