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

Right Click when no row selected - error

5 Answers 134 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Richard Newson
Top achievements
Rank 2
Richard Newson asked on 14 Mar 2011, 05:29 AM
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
<!--    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

5 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 14 Mar 2011, 08:27 AM
Hi Richard Newson,

I have tried to reproduce that issue, but I was not able to. I am sending you the sample project I used for testing it. Please take a look at it and let me know if it corresponds to your scenario.
 

Best wishes,
Maya
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Richard Newson
Top achievements
Rank 2
answered on 14 Mar 2011, 09:54 PM
Thanks for the quick response.
Your example fired up alright and tests fine.
However, you do not have the GridView's SelectedItem="{Binding SelectedItem, Mode=TwoWay}" set.

Once your example starts up, it selects the first item in the GridView, thus, it works per my app.
The problem occurs when there is nothing already selected in the GridView and then you right click an item.
0
Maya
Telerik team
answered on 15 Mar 2011, 08:39 AM
Hi Richard Newson,

Basically, the sample I attached previously was based on the code-snippet you provided, thus the IsSynchronizedWithCurrentItem property was set to "True". This ensures that there will be a selected item on initialization. However, even when I set the property to "False" so that there is no selected item and bind the SelectedItem, I was not able to reproduce the issue. Did you manage to get the exception on the very same project ? 
 

Regards,
Maya
the Telerik team
Registration for Q1 2011 What’s New Webinar Week is now open. Mark your calendar for the week starting March 21st and book your seat for a walk through all the exciting stuff we ship with the new release!
0
Richard Newson
Top achievements
Rank 2
answered on 15 Mar 2011, 09:57 PM
Even when making said change I do not get the exception - hmmm?
Not sure where to look now.
0
Richard Newson
Top achievements
Rank 2
answered on 15 Mar 2011, 11:49 PM
OK, now that we have got through all the above, it looks like the issue is in what is tied to the binding on the Command of one of the Menu items.
If I comment them out 1 by 1 I found the one causing issues - not sure why but I can now hit it with the correct hammer.

Found that it was the Logic test on the CanExecute of the bound RelayCommand - it was firing against the NULL (no selected item) first then it selected and assessed. Thus, it was looking for an object to assess CanExecute/IsEnabled of the NULL item before selecting the clicked item. Solved by defaulting when NULL to an appropriate state.

Thanks for the assistance.
Tags
GridView
Asked by
Richard Newson
Top achievements
Rank 2
Answers by
Maya
Telerik team
Richard Newson
Top achievements
Rank 2
Share this question
or