Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6217087
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T07:21:52+00:00 2026-05-24T07:21:52+00:00

I have a user control with its own context menu, however I need to

  • 0

I have a user control with its own context menu, however I need to add additional items to that menu.

The approach I took was to have a dependency property called ContextMenuItems:

Public Shared ReadOnly ContextMenuItemsProperty As DependencyProperty = DependencyProperty.Register("ContextMenuItems", GetType(ObservableCollection(Of MenuItem)), GetType(SmartDataControl), New FrameworkPropertyMetadata(New ObservableCollection(Of MenuItem)))
Public Property ContextMenuItems As ObservableCollection(Of MenuItem)
    Get
        Return GetValue(ContextMenuItemsProperty)
    End Get

    Set(ByVal value As ObservableCollection(Of MenuItem))
        SetValue(ContextMenuItemsProperty, value)
    End Set
End Property

I then used a CompositeCollection to combine the static menu items from the control with the list provided by the host:

    <CompositeCollection x:Key="MenuItemsCompositeCollection">
        <MenuItem Header="TEST" />
        <CollectionContainer Collection="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=ContextMenuItems, Converter={StaticResource TestConverter}}" />
        <MenuItem Header="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=ContextMenuItems}" />
    </CompositeCollection>

What I see when I bind to that resource is:

  • TEST
  • (Collection)

The second menu item is bound to the collection to prove I can get to it. I have a test converter that I have added to menu item and it breaks in the converter method, but when I add the converter to the CollectionContainer it doesn’t get called.

Finally, I get the following error in the output window:

System.Windows.Data Error: 4 : Cannot find source for binding with reference ‘RelativeSource FindAncestor, AncestorType=’System.Windows.Controls.UserControl’, AncestorLevel=’1”. BindingExpression:Path=ContextMenuItems; DataItem=null; target element is ‘CollectionContainer’ (HashCode=41005040); target property is ‘Collection’ (type ‘IEnumerable’)

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-24T07:21:53+00:00Added an answer on May 24, 2026 at 7:21 am

    That “proof” of yours does not work because the two objects compared are obviously not equal. You cannot use RelativeSource or ElementName bindings in a collection container because the necessary conditions are not met, i.e. there is no NameScope and since the CollectionContainer is an abtract object which does not appear in the visual tree there also is not parent via which the ancestor could be found.

    If you have access to the UserControl you can however use the Binding.Source and a x:Reference to the UserControl’s name, to prevent a cyclical dependecy error the CompositeCollection should be defined in the UserControl.Resources and then referenced using StaticResource.

    e.g.

    <UserControl Name="control">
        <UserControl.Resources>
            <CompositeCollection x:Key="collection">
                <!-- ... -->
                <CollectionContainer Collection="{Binding ContextMenuItems, Source={x:Reference control}, Converter=...}"/>
            </CompositeCollection>
        </UserControl.Resources>
        <!-- ... -->
            <MenuItem ItemsSource="{Binding Source={StaticResource collection}}"/>
    </UserControl>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a user control in a repeater that I need to pass data
I am used to ASP.NET where each user control would have its own codebehind,
I have user control named DateTimeUC which has two textboxes on its markup: <asp:TextBox
I have a user control that I'm building. It's purpose is to display the
I have a user control that is pretty basic. It contains several TextBox controls,
I have a user control that has 2 controls A and B within it.
In my C# Windows Forms application, I have a user control that contains other
I have WPF window that uses a dockpanel and the menu control. I have
I have to use a user control form In that form, there is a
I have user control in my MVC2 app placed in the Content folder (it's

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.