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 6850199
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T01:05:41+00:00 2026-05-27T01:05:41+00:00

This question is a sequel to this question (I have applied the answer, but

  • 0

This question is a “sequel” to this question (I have applied the answer, but it still won’t work).

I’m trying to create an extended ToolBar control for a modular application, which can load its items from multiple data sources (but that is not the issue I’m trying to solve right now, now I want it to work when used as regular ToolBar found in WPF).

In short: I want the ToolBar’s items to be able to bind to the tb:ToolBar’s parents.

I have following XAML code:

<Window Name="myWindow" DataContext="{Binding ElementName=myWindow}" >
    <DockPanel>
        <tb:ToolBar Name="toolbar" DockPanel.Dock="Top" DataContext="{Binding ElementName=myWindow}>
            <tb:ToolBar.Items>
                <tb:ToolBarControl Priority="-3">
                    <tb:ToolBarControl.Content>
                        <StackPanel Orientation="Horizontal">
                            <TextBlock>Maps:</TextBlock>
                            <ComboBox ItemsSource="{Binding SomeProperty, ElementName=myWindow}">

Some info about the types:

  • tb:ToolBar is an UserControl with dependency property Items of type FreezableCollection<ToolBarControl>.

  • tb:ToolBarControl is an UserControl with template pretty much identical to ContentControl’s template.

The problem is that the binding in the ComboBox fails (with the usual “Cannot find source for binding with reference”), because its DataContext is null.

Why?

EDIT: The core of the question is “Why is the DataContext not inherited?”, without it, the bindings can’t work.

EDIT2:

Here is XAML for the tb:ToolBar:

<UserControl ... Name="toolBarControl">
    <ToolBarTray>
        <ToolBar ItemsSource="{Binding Items, ElementName=toolBarControl}" Name="toolBar" ToolBarTray.IsLocked="True" VerticalAlignment="Top" Height="26">

EDIT 3:

I posted an example of what works and what doesn’t: http://pastebin.com/Tyt1Xtvg

Thanks for your answers.

  • 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-27T01:05:42+00:00Added an answer on May 27, 2026 at 1:05 am

    I personally don’t like the idea of setting DataContext in controls. I think doing this will somehow break the data context inheritance. Please take a look at this post. I think Simon explained it pretty well.

    At least, try removing

    DataContext="{Binding ElementName=myWindow}"
    

    from

    <tb:ToolBar Name="toolbar" DockPanel.Dock="Top" DataContext="{Binding ElementName=myWindow}> 
    

    and see how it goes.

    UPDATE

    Actually, keep all your existing code (ignore my previous suggestion for a moment), just change

    <ComboBox ItemsSource="{Binding SomeProperty, ElementName=myWindow}"> 
    

    to

    <ComboBox ItemsSource="{Binding DataContext.SomeProperty}"> 
    

    and see if it works.

    I think because of the way you structure your controls, the ComboBox is at the same level/scope as the tb:ToolBarControl and the tb:ToolBar. That means they all share the same DataContext, so you don’t really need any ElementName binding or RelativeSource binding to try to find its parent/ancestor.

    If you remove DataContext="{Binding ElementName=myWindow} from the tb:ToolBar, you can even get rid of the prefix DataContext in the binding. And this is really all you need.

    <ComboBox ItemsSource="{Binding SomeProperty}"> 
    

    UPDATE 2 to answer your Edit 3

    This is because your Items collection in your tb:ToolBar usercontrol is just a property. It’s not in the logical and visual tree, and I believe ElementName binding uses logical tree.

    That’s why it is not working.

    Add to logical tree

    I think to add the Items into the logical tree you need to do two things.

    First you need to override the LogicalChildren in your tb:ToolBar usercontrol.

        protected override System.Collections.IEnumerator LogicalChildren
        {
            get
            {
                if (Items.Count == 0)
                {
                    yield break;
                }
    
                foreach (var item in Items)
                {
                    yield return item;
                }
            }
        }
    

    Then whenever you added a new tb:ToolBarControl you need to call

    AddLogicalChild(item);
    

    Give it a shot.

    This WORKS…

    After playing around with it a little bit, I think what I showed you above isn’t enough. You will also need to add these ToolBarControls to your main window’s name scope to enable ElementName binding. I assume this is how you defined your Items dependency property.

    public static DependencyProperty ItemsProperty =
        DependencyProperty.Register("Items",
                                    typeof(ToolBarControlCollection),
                                    typeof(ToolBar),
                                    new FrameworkPropertyMetadata(new ToolBarControlCollection(), Callback));
    

    In the callback, it is where you add it to the name scope.

    private static void Callback(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        var toolbar = (ToolBar)d;
        var items = toolbar.Items;
    
        foreach (var item in items)
        {
            // the panel that contains your ToolBar usercontrol, in the code that you provided it is a DockPanel
            var panel = (Panel)toolbar.Parent;
            // your main window
            var window = panel.Parent;
            // add this ToolBarControl to the main window's name scope
            NameScope.SetNameScope(item, NameScope.GetNameScope(window));
    
            // ** not needed if you only want ElementName binding **
            // this enables bubbling (navigating up) in the visual tree
            //toolbar.AddLogicalChild(item);
        }
    }
    

    Also if you want property inheritance, you will need

    // ** not needed if you only want ElementName binding **
    // this enables tunneling (navigating down) in the visual tree, e.g. property inheritance
    //protected override System.Collections.IEnumerator LogicalChildren
    //{
    //    get
    //    {
    //        if (Items.Count == 0)
    //        {
    //            yield break;
    //        }
    
    //        foreach (var item in Items)
    //        {
    //            yield return item;
    //        }
    //    }
    //}
    

    I have tested the code and it works fine.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This question seems to have been asked a lot but all were trying to
This question is related to that one. But somewhat extended. Let's assume we have
This question was very helpful, however I have a list control in my report,
This is a sequel to my (other) dumb question that I have asked today
This question is specific to Erlang, but may have general implications to other IO
Note: This is a sequel to my previous question about powersets. I have got
This is the sequel to this question . I would like to combine three
This question and answer shows how to send a file as a byte array
This question might look naive, but I couldn't understand this code in the ViewModelLocator.cs
This question has been asked a lot of times in many forums. But I

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.