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

Nested ContextMenu - Element is already the child of another element.

3 Answers 97 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Jay Sanderson
Top achievements
Rank 1
Jay Sanderson asked on 31 Oct 2010, 11:37 AM

I have a contextmenu nested inside a gridview.


<my:GridViewDataColumn.CellTemplate>
    <DataTemplate>
        <StackPanel Orientation="Horizontal">
            <TextBlock x:Name="txtStockName" Text="{Binding DisplayName}" Foreground="White">
                <telerik1:RadContextMenu.ContextMenu>
                    <telerik1:RadContextMenu x:Name="menu" Opened="RadContextMenu_Opened" ItemsSource="{Binding Path=AddToListMenuItems, Source={StaticResource stockSearchViewModel}}">                                                                                                                
                    </telerik1:RadContextMenu>
                </telerik1:RadContextMenu.ContextMenu>
            </TextBlock>
        </StackPanel>
    </DataTemplate>
</my:GridViewDataColumn.CellTemplate>

The contextmenu is bound to a collection in the ViewModel.

I have added the following to the view code behind :

private void RadContextMenu_Opened(object sender, RoutedEventArgs e)
{
    try
    {
        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;
        }                
    }
    catch (Exception ex)
    {
          
        throw ex;
    }

I am finding that the menu works successfully the first time around, but on the second attempt I get the error -

Webpage error details
  
User Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E; InfoPath.2; Media Center PC 6.0; OfficeLiveConnector.1.5; OfficeLivePatch.1.3)
Timestamp: Sun, 31 Oct 2010 10:35:21 UTC
  
  
Message: Unhandled Error in Silverlight Application 
Code: 4004    
Category: ManagedRuntimeError       
Message: System.InvalidOperationException: Element is already the child of another element.
   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at MS.Internal.XcpImports.Collection_AddValue[T](PresentationFrameworkCollection`1 collection, CValue value)
   at MS.Internal.XcpImports.Collection_AddDependencyObject[T](PresentationFrameworkCollection`1 collection, DependencyObject value)
   at System.Windows.PresentationFrameworkCollection`1.AddDependencyObject(DependencyObject value)
   at System.Windows.Controls.UIElementCollection.AddInternal(UIElement value)
   at System.Windows.Controls.ItemsControl.AddVisualChild(Int32 containerIndex, DependencyObject container, Boolean needPrepareContainer)
   at System.Windows.Controls.ItemsControl.AddContainers()
   at System.Windows.Controls.ItemsControl.RecreateVisualChildren(IntPtr unmanagedObj)     
  
Line: 56
Char: 13
Code: 0
URI: http://localhost/DLTWeb/DLT.SLTestPage.aspx

Can someone give me some pointers as to where I'm going wrong ?

Kind Regards,

Jay Sanderson

3 Answers, 1 is accepted

Sort by
0
Maya
Telerik team
answered on 01 Nov 2010, 10:59 AM
Hi Jay Sanderson,

I tried to reproduce the issue you specified with the Mozilla browser, but I was not able to. I am sending you the sample project I test the problem on. Please take a look and let me know if there is some misunderstandings according to your requirements.

 
Kind regards,
Maya
the Telerik team
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Public Issue Tracking system and vote to affect the priority of the items
0
Jay Sanderson
Top achievements
Rank 1
answered on 10 Nov 2010, 10:47 AM
Hi Maya,


Apologies for my delay in response. My problem is not so much with the Mozilla browser, I'm using this in IE.

I need to have a context menu that is databound to a collection outside of the data grid, which can pass a selected menu item, along with a selected grid item to a command property in the bound ViewModel.

This works with first selection/action using the code submitted in my first post, but causes the error on the second selection/action.

Cheers,,

Jay
0
Maya
Telerik team
answered on 15 Nov 2010, 06:24 PM
Hi Jay Sanderson,

May you try to replicate your issue on the sample project I sent to you or sent your own sample application (via support ticket) ? Furthermore, why do you require to add your ContextMenu in the CellTemplate of the column ? You may try to add it to the rows as demonstrated in our demos.

Kind regards,
Maya
the Telerik team
See What's New in RadControls for Silverlight in Q3 2010 on Tuesday, November 16, 2010 11:00 AM - 12:00 PM EST or 10:00 PM - 11:00 PM EST: Register here>>
Tags
GridView
Asked by
Jay Sanderson
Top achievements
Rank 1
Answers by
Maya
Telerik team
Jay Sanderson
Top achievements
Rank 1
Share this question
or