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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T16:23:35+00:00 2026-06-12T16:23:35+00:00

Say I have two classes that can reference a third UI object (in this

  • 0

Say I have two classes that can reference a third UI object (in this example a button).

In addition, the parent class can contain an element of the child class.

If they both are bound to the same control, the same way, the child will fail but the parent succeed.

Is this a bug in WPF?


The parent :

class MyFrameworkElement : FrameworkElement
{
    // A depenedency property that will contain a child element sub-element
    private static readonly DependencyProperty ChildElementProperty =
                    DependencyProperty.Register("ChildElement",
                    typeof(MyChildElement),
                    typeof(MyFrameworkElement),
                    new PropertyMetadata());

    [Category("ChildProperties")]
    public MyChildElement ChildElement
    {
        set { SetValue(ChildElementProperty, value); }
        get { return (MyChildElement)GetValue(ChildElementProperty); }
    }


    // Now, a reference to some other control, in this case we will bind a button to it!
    public UIElement ButtonReferenceInParent
    {
        get { return (UIElement)GetValue(ButtonReferenceInParentProperty); }
        set { SetValue(ButtonReferenceInParentProperty, value); }
    }

    // Using a DependencyProperty as the backing store for ButtonReferenceInParent.  This enables animation, styling, binding, etc...
    public static readonly DependencyProperty ButtonReferenceInParentProperty =
        DependencyProperty.Register("ButtonReferenceInParent", typeof(UIElement), typeof(MyFrameworkElement), new UIPropertyMetadata(null));

And then the child :

public class MyChildElement : FrameworkElement
{
    public UIElement ButtonReferenceInChild
    {
        get { return (UIElement)GetValue(ButtonReferenceInChildProperty); }
        set { SetValue(ButtonReferenceInChildProperty, value); }
    }

    public static readonly DependencyProperty ButtonReferenceInChildProperty =
        DependencyProperty.Register("ButtonReferenceInChild", typeof(UIElement), typeof(MyChildElement), new UIPropertyMetadata(null));
}

OK –

Now say I Add them to my XAML like this :

<Grid>
    <my:MyFrameworkElement x:Name="ParentName" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" ButtonReferenceInParent="{Binding ElementName=buttonisme}">
        <my:MyFrameworkElement.ChildElement>
            <my:MyChildElement x:Name="ChildName" ButtonReferenceInChild="{Binding ElementName=buttonisme}"/>
        </my:MyFrameworkElement.ChildElement>
    </my:MyFrameworkElement>
    
    <Button x:Name="buttonisme" Click="buttonisme_Click" />
</Grid>

Why does the binding work on the parent but then fail on the child, when I am using the EXACT same notation?


Here is my test code…

     Console.WriteLine("Parent button reference is {0}", ParentName.ButtonReferenceInParent);

        if (ChildName.ButtonReferenceInChild == null)
        {
            Console.WriteLine("Child button reference is null!");
        } 
        else
        {
            Console.WriteLine("Child button is {0}", ChildName.ButtonReferenceInChild);
        }

And here is the test result…

Parent button reference is System.Windows.Controls.Button

Child button reference is null!

  • 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-06-12T16:23:37+00:00Added an answer on June 12, 2026 at 4:23 pm

    The short answer to a long question is that Microsoft doesn’t expect you to derive from FrameworkElement without doing a little plumbing.

    Just doing derivation, breaks the logical tree which is used when doing binding by element name.

    You probably also have to plum up the visual tree, and overload the arrange/measure parts of framework element. (We don’t do that here as we aren’t visual in the example.)

    In this specific case we need to add any children of your object to the logical tree or break the ability to bind child elements.

    A good example of someone who solved this is here

    Information on overriding the logical tree is here

    Anyways, the code needed to fix this SIMPLE example only relied on the logical tree (as the child object isn’t really visual.)

    Adding this function and changing the dependency property makes the binding work.

            private static readonly DependencyProperty ChildElementProperty =
                        DependencyProperty.Register("ChildElement",
                        typeof(MyChildElement),
                        typeof(MyFrameworkElement),
                        new PropertyMetadata(OnChildElementChanged));
    
        private static void OnChildElementChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            MyFrameworkElement control = d as MyFrameworkElement;
    
            if (e.OldValue != null)
            {
                control.RemoveLogicalChild(e.OldValue);
            }
    
            control.AddLogicalChild(e.NewValue);
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have two classes that look like this: public class ByteFilter {
So say, for example I have two classes: Base = declarative_base() class Vendor(Base): __tablename__
Say I have two classes: class A(db.Model): class B(db.Model): a_reference = ReferenceProperty(A) I can
Let's say that I have two classes A and B. class A { private:
Say I have two classes and have a requirement that the primary key property
Say I have two classes, and neither of them are GUI components. Class A
Say you have two classes, order and customer: public class Customer{ public int CustomerId
Say I have two separate classes, A and B. I also have Repository class
Let's say we have these two classes: public class Base { public static int
Say i have the following two classes: public class User { public int ID

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.