ContextMenu as static resource

7 posts, 0 answers
  1. AlinRaul
    AlinRaul avatar
    10 posts
    Member since:
    Jul 2009

    Posted 16 Aug 2010 Link to this post

    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








  2. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 17 Aug 2010 Link to this post

    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
  3. DevCraft banner
  4. AlinRaul
    AlinRaul avatar
    10 posts
    Member since:
    Jul 2009

    Posted 17 Aug 2010 Link to this post

    Hi Hristo,

    Thanks for your answer.

    Alin
  5. Simon
    Simon avatar
    47 posts
    Member since:
    May 2008

    Posted 23 Jan 2011 Link to this post

    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 
  6. Hristo
    Admin
    Hristo avatar
    832 posts

    Posted 24 Jan 2011 Link to this post

    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>>
  7. yjh
    yjh avatar
    67 posts
    Member since:
    Aug 2007

    Posted 05 Aug 2011 Link to this post

    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.
  8. Konstantina
    Admin
    Konstantina avatar
    2332 posts

    Posted 10 Aug 2011 Link to this post

    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 >>

Back to Top
DevCraft banner