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

  • Home
  • SEARCH
  • 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 3341308
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T00:42:28+00:00 2026-05-18T00:42:28+00:00

This is weird. I have a UserControl (MyControl) with a button inside. I have

  • 0

This is weird. I have a UserControl (MyControl) with a button inside. I have added a command to this button whose command target is another user control which is again added to the same window.

When I add the UserControl statically in xaml to the CustomControl’s host space, the CommandBinding system works fine, where as the same doesn’t work if the UserControl is added programatically (on Loaded event of this Window).

What could be the reason. Am I missing something in here ?!?

Update:

*A better representation of my words as an image. Also I have uploaded my source code with binaries @ https://code.google.com/p/commandbindingsample/ and at

http://cid-6fc1e241d4534589.office.live.com/embedicon.aspx/Wpf%20Samples/CommandBinding.zip (not versioned) *

alt text

  • 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-18T00:42:28+00:00Added an answer on May 18, 2026 at 12:42 am

    In your user control, you’re setting the CommandBinding like this:

    CommandTarget="{Binding ElementName=userControl11}"
    

    If you look in the Output window while your program’s running, you’ll see this:

    System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=userControl11'. BindingExpression:(no path); DataItem=null; target element is 'Button' (Name=''); target property is 'CommandTarget' (type 'IInputElement')
    

    This is because there’s no element named userControl11 in the current namescope. See this for full details on how XAML namescopes work, but in short, XAML names are only visible within the page, window, or user control that they’re declared in. You can’t reference a name declared in your window from inside a user control.

    If you want to be able to set a command target on a user control via binding, it needs to be exposed as a dependency property. You’d add a declaration like this to the control:

    public IInputElement CommandTarget
    {
        get { return (IInputElement)GetValue(CommandTargetProperty); }
        set { SetValue(CommandTargetProperty, value); }
    }
    
    public static readonly DependencyProperty CommandTargetProperty =
            DependencyProperty.Register("CommandTarget", typeof(IInputElement), typeof(UserControl1), new UIPropertyMetadata(null));
    

    and in the user control’s XAML, bind to this property:

    <Button Content="Click" 
            Command="local:Commands.ClickCommand" 
            CommandTarget="{Binding RelativeSource={RelativeSource AncestorType=UserControl}, Path=CommandTarget}" />
    

    I can’t actually get this to work in your project, but that’s because I can’t figure your project out. You seem to have two or three user controls named UserControl1 in different namespaces, and the names of the files in the project don’t correspond to their contents, and there’s not very much in the way of useful commentary. (Just in general, instead of saying “this doesn’t work,” describe the behavior you’re expecting; that makes it a lot clearer to someone trying to help you what your actual problem might be.)

    I hope this is helpful anyway.

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

Sidebar

Related Questions

This could be weird, Have you ever come across a blog which you wanted
This is a weird problem I have started having recently. My team is developing
This is pretty weird. I have my Profiler open and it obviously shows that
This one is weird, I have a page that consists of a html table
This is a really weird problem that I have been having. When I download
This is the day of weird behavior. We have a Win32 project made with
This is kind of a weird problem, but I have to create a search
Sounds like a weird question, but say I have something like this: $.post( /myajax.php,
I've seen this weird behavior on several sites recently: I scroll down a page
I've got this weird problem - I'm calling ChangeServiceConfig on a newly installed service

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.