Your Privacy Matters: We use our own and third-party cookies to improve your experience on our website. By continuing to use the website we understand that you accept their use. Cookie Policy
215
Get Parent Index - XamDatagrid
posted

I have Parent and Child records. I have created context menu in C#. When user right click on child record, i want to get parent record index. How to get that in c#..?

  • 2677
    Verified Answer
    posted

    Hello Ragunath,

    I'm not sure how far you went with the whole process, but I have a solution here that starts at the beginning. 

    I dug into the DataRecordPresenterStyle and set its context menu to a context menu resource.  I was getting some errors trying to se the ContextMenu up directly in Setter.Value section.  For the ContextMenu, I handle the Opened event for later use.  You will also notice the key of the ContextMenu is GridContextMenu which is linked to the ContextMenu Value property in the style setter below. 

     <Grid.Resources>
                <ContextMenu x:Key="GridContextMenu" Opened="ContextMenu_Opened">
                    <MenuItem Header="Hello"/>
                    <MenuItem Header="Goodbye"/>
                </ContextMenu>
                <Style TargetType="{x:Type igDP:DataRecordPresenter}" x:Key="ContextMenuRecordStyle1">
                    <Setter Property="ContextMenu" Value="{DynamicResource GridContextMenu}" />
                </Style>
            </Grid.Resources>

    Then in the event handler, we can get the datacontext of the menu which is the DataRecord itself.  Then we can get the Parent record using the property ParentDataRecord from there. 

    private void ContextMenu_Opened(object sender, RoutedEventArgs e)
            {
                ContextMenu cm = e.OriginalSource as ContextMenu;
                DataRecord dr = cm.DataContext as DataRecord;
                if (dr != null)
                {
                    DataRecord Parent = dr.ParentDataRecord;
                }
            }

    Finally, we have to make sure the records use the correct DataRecordPresenterStyle.  If you  set up your fieldLayouts in xaml, you can set the DataRecordPresenterStyle property and set it to the style we created above.

    <igDP:FieldLayout.Settings>
                            <igDP:FieldLayoutSettings DataRecordPresenterStyle="{StaticResource ContextMenuRecordStyle1}"/>
                        </igDP:FieldLayout.Settings>

    I hope this helps.  If you have any questions, please let me know. 

    This should do the trick.