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
Can you verify using the debugger if the exception is not inside this event handler?
Kind regards,Vlad
the Telerik team
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; }
}
}
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
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)
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