Problem

ContextMenu as StaticResource

Posted 7 years ago by Avatar Robert Croshere
Hi,

When a context menu is placed in the resources part as follows:
    <UserControl.Resources>
        <shared:ContextMenu
            x:Key="menu">
            <shared:MenuItem
                Header="contexttest" />
        </shared:ContextMenu>
    </UserControl.Resources>
And referred to like this:
shared:ContextMenuService.ContextMenu="{StaticResource menu}"
Every time the contextmenu is invoked, an exception will occur that will crash the app with the following details:

An unhandled exception ('Unhandled error in Silverlight Application
Code: 4004
Category: ManagedRuntimeError
Message: System.ArgumentException: Value does not fall within the expected range.

If the contextmenu is placed directly within the control (thus not using a static resource) it works as expected. However, I'm using the context menu in a DataTemplate for a listbox that can contain many items and I don't want the contextmenu to be created for each item, thats why I'm using a static resource.

Regards,
Robert Croshere

Comments (3)

Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Robert,

The ContextMenu is a visual and therefore can only have 1 parent. So unfortunately, you cannot reuse it like you show above.

Actipro Software Support
Posted 7 years ago by Robert Croshere
Ok,

I guess I would have expected a different error message. Might be a good idea to state that in de documentation as it differs from how WPF works (and they are meant to be similar).

thanks,
Robert
Posted 7 years ago by Actipro Software Support - Cleveland, OH, USA
Hi Robert,

Silverlight is notoriously bad at providing useful error messages. We've updated our documentation to indicate that the ContextMenu cannot be reused.

Actipro Software Support
Information The latest build of this product (2018.1 build 0230) was released 10 days ago, which was after the last post in this thread.

Add a Comment

Please log in to a validated account to post comments.