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

ContextMenu as static resource

6 Answers 154 Views
Menu
This is a migrated thread and some comments may be shown as answers.
AlinRaul
Top achievements
Rank 1
AlinRaul asked on 16 Aug 2010, 10:12 AM
Hi guys,

I have the following code:

<Rectangle Fill="Transparent" telerik:RadContextMenu.ContextMenu="{StaticResource contextMenu2}">

where the contextMenu2 is a resource defined as follows:

<telerik:RadContextMenu x:Key="contextMenu2">
                       <telerik:RadMenuItem Header="Copy" />
                       <telerik:RadMenuItem Header="Paste" />
                       <telerik:RadMenuItem Header="Cut" />
</telerik:RadContextMenu>

The problem is that if I use the context menu this way - as a static resource - it crashes with the following exception:

System.ArgumentException: Value does not fall within the expected range.
   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh)
   at System.Windows.DependencyObject.SetValue(DependencyProperty property, DependencyObject doh)
   at System.Windows.Controls.Primitives.Popup.set_Child(UIElement value)
   at Telerik.Windows.Controls.RadContextMenu.CreateRootPopup(Popup popup, UIElement child)
   at Telerik.Windows.Controls.RadContextMenu.HookupRootPopup()
   at Telerik.Windows.Controls.RadContextMenu.OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Telerik.Windows.Controls.RadContextMenu.set_IsOpen(Boolean value)
   at Telerik.Windows.Controls.RadContextMenu.TriggerEventFired()
   at Telerik.Windows.Controls.RadContextMenu.OnEventTriggered(Object sender, EventArgs e)
   at Telerik.Windows.Controls.RadContextMenu.OnElementWithContextMenuMouseClick(Object sender, MouseButtonEventArgs e)
   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)}
    [System.ArgumentException]: {System.ArgumentException: Value does not fall within the expected range.
   at MS.Internal.XcpImports.CheckHResult(UInt32 hr)
   at MS.Internal.XcpImports.SetValue(IManagedPeerBase obj, DependencyProperty property, DependencyObject doh)
   at System.Windows.DependencyObject.SetValue(DependencyProperty property, DependencyObject doh)
   at System.Windows.Controls.Primitives.Popup.set_Child(UIElement value)
   at Telerik.Windows.Controls.RadContextMenu.CreateRootPopup(Popup popup, UIElement child)
   at Telerik.Windows.Controls.RadContextMenu.HookupRootPopup()
   at Telerik.Windows.Controls.RadContextMenu.OnIsOpenChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
   at System.Windows.DependencyObject.RaisePropertyChangeNotifications(DependencyProperty dp, Object oldValue, Object newValue)
   at System.Windows.DependencyObject.UpdateEffectiveValue(DependencyProperty property, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, ValueOperation operation)
   at System.Windows.DependencyObject.SetValueInternal(DependencyProperty dp, Object value, Boolean allowReadOnlySet)
   at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value)
   at Telerik.Windows.Controls.RadContextMenu.set_IsOpen(Boolean value)
   at Telerik.Windows.Controls.RadContextMenu.TriggerEventFired()
   at Telerik.Windows.Controls.RadContextMenu.OnEventTriggered(Object sender, EventArgs e)
   at Telerik.Windows.Controls.RadContextMenu.OnElementWithContextMenuMouseClick(Object sender, MouseButtonEventArgs e)
   at MS.Internal.CoreInvokeHandler.InvokeEventHandler(Int32 typeIndex, Delegate handlerDelegate, Object sender, Object args)
   at MS.Internal.JoltHelper.FireEvent(IntPtr unmanagedObj, IntPtr unmanagedObjArgs, Int32 argsTypeIndex, String eventName)

This would not happen if I set the context menu in the usual way:

<Rectangle Fill="Transparent">
                 <telerik:RadContextMenu.ContextMenu>
                     <telerik:RadContextMenu>
                         <telerik:RadMenuItem Header="Copy" />
                         <telerik:RadMenuItem Header="Paste" />
                         <telerik:RadMenuItem Header="Cut" />
                     </telerik:RadContextMenu>
                 </telerik:RadContextMenu.ContextMenu>                 
</Rectangle>

Please tell me what can I do in order to use the context menu as a static resource, as in the first example?

Thanks,
AlinRaul








6 Answers, 1 is accepted

Sort by
0
Hristo
Telerik team
answered on 17 Aug 2010, 08:21 AM
Hello Alin,

Using RadContextMenu as a static resource is not supported by the platform. There is a limitation in the framework that prevents a resource to get a parent (in your case RadContextMenu must be placed in a popup control).

Let us know if you need more information.

Regards,
Hristo
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
AlinRaul
Top achievements
Rank 1
answered on 17 Aug 2010, 10:08 AM
Hi Hristo,

Thanks for your answer.

Alin
0
Simon
Top achievements
Rank 1
answered on 24 Jan 2011, 02:21 AM
Is this going to be sorted soon?

Doesn't seem right that we can't do this. I've created a RadContextMenu in code and then attached to my control (in my case a RadTreeView) and it worked fine - so I don't really understand why it can't be done using XAML declare as a StaticResource then attached to a UserControl using the SetContextMenu method.

Thanks 
0
Hristo
Telerik team
answered on 24 Jan 2011, 10:12 AM
Hi Simon,

When you define some control in ResourceDictionary and then try to display it (like RadContextMenu) you get an exception that this control already have parent. This behavior is not correct (the context menu is in the resource so it doesn't have a parent) but we cannot fix it (or workaround it) - e.g. if we remove it from the resources then all StaticResource markup extensions pointing to it will fail. We have notified Microsoft about this limitation.

On the other hand RadContextMenu is not designed to be shareable (even if the platform supports it) so it is not recommended to attach the same instance of RadContextMenu on multiple elements. It will not work correctly.

We may consider improving this functionality if there is enough demand and when the platform supports it.

Greetings,
Hristo
the Telerik team
Let us know about your Windows Phone 7 application built with RadControls and we will help you promote it. Learn more>>
0
yjh
Top achievements
Rank 1
answered on 05 Aug 2011, 09:38 AM
As we know,RadContextMenu is not designed to be shareable.

But RadMenuItems in RadContextMenu can be a static resource ?

If can,please provide some code snippet, because the "Items" property is readonly.
0
Konstantina
Telerik team
answered on 10 Aug 2011, 12:03 PM
Hello Yao,

The RadMenuItems also cannot be shared. However, you could create a collection with items. Define it in the resources of the page and set the ItemsSource to an instance of ContextMenu.

Greetings,
Konstantina
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get now >>

Tags
Menu
Asked by
AlinRaul
Top achievements
Rank 1
Answers by
Hristo
Telerik team
AlinRaul
Top achievements
Rank 1
Simon
Top achievements
Rank 1
yjh
Top achievements
Rank 1
Konstantina
Telerik team
Share this question
or