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