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

Integrating Context Menu with Grid

3 Answers 521 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Ruban
Top achievements
Rank 1
Ruban asked on 22 Sep 2014, 06:09 PM
I have a muliple hierarchy Gridview with a context menu and I would like to pass the current row from the grid when clicking a particular item from grid, however when I do check check for the method handler in the VM, the parameter is always null.
What I would like to do is get access to the Row datacontext in the grid when a user clicks on the menu.
XAML code:
<telerik:RadGridView RowIndicatorVisibility="Hidden" SelectionMode="Single" SelectionUnit="FullRow" SelectedCellsChanged="RadGridView_OnSelectedCellsChanged" x:Name="radGridView" Margin="10" ItemsSource="{Binding Enquiries}" ShowGroupPanel="False" AutoGenerateColumns="False" Height="380" IsFilteringAllowed="False" DataLoaded="RadGridView1_OnDataLoaded">
          <i:Interaction.Triggers>
            <i:EventTrigger EventName="Sorting">
              <cmd:EventToCommand Command="{Binding SortingCommand}" PassEventArgsToCommand="True"></cmd:EventToCommand>
            </i:EventTrigger>
          </i:Interaction.Triggers>
          <telerik:RadContextMenu.ContextMenu>
              <telerik:RadContextMenu>
                    <i:Interaction.Triggers>
                      <i:EventTrigger EventName="ItemClick">
                        <cmd:EventToCommand Command="{Binding GetEnquiryCommand}" CommandParameter="{Binding ElementName=radGridView, Path=SelectedRow}"></cmd:EventToCommand>
                      </i:EventTrigger>
                    </i:Interaction.Triggers>                <telerik:RadMenuGroupItem Background="LightSkyBlue">
                  <telerik:RadMenuItem Header="Mark as Complete"></telerik:RadMenuItem>
                  <telerik:RadMenuItem Header="Link to UW Reference"></telerik:RadMenuItem>
                </telerik:RadMenuGroupItem>
                <telerik:RadMenuGroupItem Background="LightSkyBlue">
                  <telerik:RadMenuItem Header="Edit Enquiry">
                  </telerik:RadMenuItem>
                  <telerik:RadMenuItem Header="View Enquiry">
                  </telerik:RadMenuItem>
                </telerik:RadMenuGroupItem>
              </telerik:RadContextMenu>
          </telerik:RadContextMenu.ContextMenu>
          <telerik:RadGridView.ChildTableDefinitions>
            <telerik:GridViewTableDefinition/>
          </telerik:RadGridView.ChildTableDefinitions>
          <telerik:RadGridView.Columns>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding EnquiryId}" Header="ID" IsCustomSortingEnabled="True" HeaderCellStyle="{StaticResource HeaderCellStyle}"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding AssuredName}" Header="Assured" IsCustomSortingEnabled="True" HeaderCellStyle="{StaticResource HeaderCellStyle}"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding RaterModelVersion.RaterModel.RaterModelName}" Header="Product" IsCustomSortingEnabled="True" HeaderCellStyle="{StaticResource HeaderCellStyle}"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding BrokerName}" Header="Broker Name" IsCustomSortingEnabled="True" HeaderCellStyle="{StaticResource HeaderCellStyle}"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding EnquiryStatus}" Header="Status"  IsSortable="False" HeaderCellStyle="{StaticResource HeaderCellStyle}"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Underwriter}" Header="UW"  IsSortable="False" HeaderCellStyle="{StaticResource HeaderCellStyle}"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding ChangedDate}" Header="Modified Date" IsCustomSortingEnabled="True" HeaderCellStyle="{StaticResource HeaderCellStyle}"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding ChangedBy}" Header="Modified By"  IsSortable="False" HeaderCellStyle="{StaticResource HeaderCellStyle}"/>
            <telerik:GridViewDataColumn DataMemberBinding="{Binding Comments}" Header="UW Comments"  IsSortable="False" HeaderCellStyle="{StaticResource HeaderCellStyle}" Width="*"/>
          </telerik:RadGridView.Columns>
          <telerik:RadGridView.SortDescriptors>
            <telerik:ColumnSortDescriptor  Column="{Binding Columns[\AssuredName\], ElementName=radGridView}"
                             SortDirection="Ascending" />
          </telerik:RadGridView.SortDescriptors>
          <telerik:RadGridView.HierarchyChildTemplate>
            <DataTemplate>
              <telerik:RadGridView RowIndicatorVisibility="Hidden" ItemsSource="{Binding SectionLayers}" AutoGenerateColumns="False" ShowGroupPanel="False" CanUserSortColumns="False" IsFilteringAllowed="False" DataLoaded="RadGridView1_OnDataLoaded" Width="Auto">
                <telerik:RadGridView.Columns>
                  <telerik:GridViewDataColumn DataMemberBinding="{Binding PolicyReference}" Header="UW Reference" />
                  <telerik:GridViewDataColumn DataMemberBinding="{Binding SegmentSectionLayer}" Header="Identifiers"/>
                  <telerik:GridViewDataColumn DataMemberBinding="{Binding LimitAt100Percent}" Header="Limits"/>
                  <telerik:GridViewDataColumn DataMemberBinding="{Binding ExcessAt100Percent}" Header="Excess"/>
                </telerik:RadGridView.Columns>
                <telerik:RadGridView.ChildTableDefinitions>
                  <telerik:GridViewTableDefinition/>
                </telerik:RadGridView.ChildTableDefinitions>
                <telerik:RadGridView.HierarchyChildTemplate>
                  <DataTemplate>
                    <telerik:RadGridView RowIndicatorVisibility="Hidden" ItemsSource="{Binding PremiumCurrencySplits}" AutoGenerateColumns="False" CanUserSortColumns="False" ShowGroupPanel="False" IsFilteringAllowed="False">
                      <telerik:RadGridView.Columns>
                          <telerik:GridViewDataColumn DataMemberBinding="{Binding PremiumCurrency}" Header="Premium Currency"/>
                          <telerik:GridViewDataColumn DataMemberBinding="{Binding ModelPrice}" Header="Model Price"/>
                          <telerik:GridViewDataColumn DataMemberBinding="{Binding TechnicalPrice}" Header="Tech Price"/>
                          <telerik:GridViewDataColumn DataMemberBinding="{Binding GrossGrossWrittenPremiumAt100Percentage}" Header="100% WRT Premium"/>
                          <telerik:GridViewDataColumn DataMemberBinding="{Binding ProjectedNetLossRatio}" Header="PLR"/>
                       </telerik:RadGridView.Columns>
                    </telerik:RadGridView>
                  </DataTemplate>
                </telerik:RadGridView.HierarchyChildTemplate>
              </telerik:RadGridView>
            </DataTemplate>
          </telerik:RadGridView.HierarchyChildTemplate>
        </telerik:RadGridView>


Any ideas ?  Here's my VM code:

private async void GetEnquiry(object enquiry) { }

3 Answers, 1 is accepted

Sort by
0
Yoan
Telerik team
answered on 23 Sep 2014, 02:53 PM
Hi,

Please check the Row Context Menu online demo for a reference. You can find the same example in your local copy of WPF demos.

Regards,
Yoan
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
0
Ruban
Top achievements
Rank 1
answered on 25 Sep 2014, 03:11 PM
Not a very useful response, had to raise a ticket for this to be answered correctly.
0
Kalin
Telerik team
answered on 26 Sep 2014, 11:47 AM
Hi Ruban,

I can see this a duplicate of Ticket with ID 862158 which has already been answered. I'll paste my reply here in order to available to the community:

The reason for the explained behavior is that ContextMenu is located in another Visual Tree and the binding of the CommandParameter is not working at all. In order to get it working you should implement it the following way (you would need to use the UIElement property of the ContextMenu which holds a reference to the GridView):

CommandParameter="{Binding Path=UIElement.SelectedItem, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=telerik:RadContextMenu}}"

You would also need to use the SelectedItem property of the GridView in order to get the data item associated with the current row.

Hope this helps.

Regards,
Kalin
Telerik
 
Check out Telerik Analytics, the service which allows developers to discover app usage patterns, analyze user data, log exceptions, solve problems and profile application performance at run time. Watch the videos and start improving your app based on facts, not hunches.
 
Tags
GridView
Asked by
Ruban
Top achievements
Rank 1
Answers by
Yoan
Telerik team
Ruban
Top achievements
Rank 1
Kalin
Telerik team
Share this question
or