This question is locked. New answers and comments are not allowed.
I have a Silverlight app with a RadGridView on it. I am using MVVM methodology.
When my grid loads it's items, I have it so that no item is initially selected.
If you select an item, then right click, all works accordingly.
However, if there is no selected item and you right click an item I get an error:
"An unhandled exception ('Unhandled Error in Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.NullReferenceExcepbon: Object reference not set to an
instance of an object."
It appears that it is not first selecting the item, doesn't reach the Code Behind "RadContextMenu_Opened(...)"
What am I doing wrong and how might I solve this?
Code snippet-
XAML
The code behind the XAML
Thanks Rich
When my grid loads it's items, I have it so that no item is initially selected.
If you select an item, then right click, all works accordingly.
However, if there is no selected item and you right click an item I get an error:
"An unhandled exception ('Unhandled Error in Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.NullReferenceExcepbon: Object reference not set to an
instance of an object."
It appears that it is not first selecting the item, doesn't reach the Code Behind "RadContextMenu_Opened(...)"
What am I doing wrong and how might I solve this?
Code snippet-
XAML
<!-- Wrap a Busy Indicator around this section --><telerik:RadBusyIndicator x:Name="radBusyIndicatorUC" IsBusy="{Binding Checks_IsBusy, Mode=TwoWay}" IsIndeterminate="True" BusyContent="Loading..." Grid.Row="5"> <Grid x:Name="UnresolvedChecksContentGrid"> <Grid.ColumnDefinitions> <ColumnDefinition /> </Grid.ColumnDefinitions> <!-- The Data Grid --> <telerik:RadGridView Grid.Row="0" Name="dgUnresolvedChecks" AutoGenerateColumns="False" ItemsSource="{Binding UnresolvedChecks, Mode=TwoWay}" SelectedItem="{Binding CurrentUnresolvedCheck, Mode=TwoWay}" IsSynchronizedWithCurrentItem="True" IsReadOnly="True" DataLoadMode="Asynchronous" RowIndicatorVisibility="Collapsed" Background="Transparent" Margin="14,0,14,10" RowStyleSelector="{StaticResource uc_selector}"> <!-- Add the Default sorting to be on the RecordID Descending --> <telerik:RadGridView.SortDescriptors> <telerik:SortDescriptor Member="ClientName" SortDirection="Ascending" /> <telerik:SortDescriptor Member="NSMDateTime" SortDirection="Descending" /> </telerik:RadGridView.SortDescriptors> <!-- This handles the EventToCommand functionality of GalaSoft MVVLite --> <i:Interaction.Triggers> <i:EventTrigger EventName="SelectionChanged" > <cmd:EventToCommand Command="{Binding UpdateStoredUnresolvedCheck, Mode=OneWay}" /> </i:EventTrigger> </i:Interaction.Triggers> <!-- The Rick Click Menu --> <telerik:RadContextMenu.ContextMenu> <telerik:RadContextMenu Opened="RadContextMenu_Opened" EventName="MouseRightButtonUp"> <telerik:RadContextMenu.Items> <telerik:RadMenuItem Header="No Action Req." /> <telerik:RadMenuItem Header="Client Advised" /> <telerik:RadMenuItem Header="Support Contacted" /> <telerik:RadMenuItem Header="Show Comments/History" Command="{Binding ShowCheckComment}" /> <telerik:RadMenuItem Header="Escalate" Command="{Binding EscalateCheck}"/> <telerik:RadMenuItem Header="Mark Read/Unread" Command="{Binding MarkCheckAsRead}" /> </telerik:RadContextMenu.Items> </telerik:RadContextMenu> </telerik:RadContextMenu.ContextMenu> <telerik:RadGridView.Columns> <telerik:GridViewDataColumn DataMemberBinding="{Binding ClientName}" Header="Client Site" Width="250" TextWrapping="Wrap" IsReadOnly="True" /> <telerik:GridViewDataColumn DataMemberBinding="{Binding FolderName}" Header="FolderName" Width="195" TextWrapping="Wrap" IsReadOnly="True" /> <telerik:GridViewDataColumn DataMemberBinding="{Binding CheckName}" Header="SHM Check" Width="350" TextWrapping="Wrap" IsReadOnly="True" /> <telerik:GridViewDataColumn DataMemberBinding="{Binding FailDescr}" Header="Failure" Width="450" TextWrapping="Wrap" /> <telerik:GridViewDataColumn DataMemberBinding="{Binding NSMDateTime}" Header="First Fail Date/Time" IsReadOnly="True" /> </telerik:RadGridView.Columns> </telerik:RadGridView> </Grid> </telerik:RadBusyIndicator> The code behind the XAML
private void RadContextMenu_Opened(object sender, RoutedEventArgs e) { RadContextMenu menu = (RadContextMenu)sender; GridViewRow row = menu.GetClickedElement<GridViewRow>(); if (row != null) { row.IsSelected = row.IsCurrent = true; GridViewCell cell = menu.GetClickedElement<GridViewCell>(); if (cell != null) { cell.IsCurrent = true; } } else { menu.IsOpen = false; } } Thanks Rich