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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T06:34:59+00:00 2026-05-20T06:34:59+00:00

My problem is not wiring up DependencyProperties in a UserControl . This is not

  • 0

My problem is not wiring up DependencyProperties in a UserControl. This is not an issue. When I bind a button in a UserControl to the UserControl‘s DependencyProperty called TargetCommand, the binding breaks when I set a DataContext on the UserControl. I’ve tried using FindAncestor and of course ElementName, but they only function when there is no DataContext on the UserControl.

Is there a way around this?

example:

Main Window

<Window xmlns:UserControls="clr-namespace:SomeNameSpace">
    <Grid>
         <UserControls:MyUserControl 
             TargetCommand="{Binding PathToCommand}"
             DataContext="{Binding PathToSomeModel}" />

MyUserControl Code Behind

public partial class MyUserControl : UserControl
{
    public static readonly DependencyProperty TargetCommandProperty =
        DependencyProperty.Register( "TargetCommand", typeof( ICommand ), typeof( MyUserControl ) );

    public ICommand TargetCommand
    {
        get { return (ICommand)GetValue( TargetCommandProperty ); }
        set { SetValue( TargetCommandProperty, value ); }
    }

MyUserControl – Xaml

<UserControl x:Name="root">
    <Button Command="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type UserControl}}, Path=TargetCommand}" />
    <Button Command="{Binding Path=TargetCommand, ElementName=root}" />

The RelativeSource and ElementName methods of binding in the MyUserControl both wire up correctly as long as the DataContext is not set on MyUserControl in the MainWindow. Neither work once the DataContext is set.

Is there a way to set a DataContext on MyUserControl, and still preserve the DependencyProperty Binding to TargetCommand?

  • 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-20T06:35:00+00:00Added an answer on May 20, 2026 at 6:35 am

    Where is your PathToCommand defined at? If I’m reading your example correctly, it should be somewhere higher in the VisualTree than the UserControl. In that case, you’d bind to whatever control has the DataContext containing PathToCommand and bind to DataContext.PathToCommand

    <Window xmlns:UserControls="clr-namespace:SomeNameSpace">
        <Grid x:Name="PART_Root">
             <UserControls:MyUserControl 
                 TargetCommand="{Binding ElementName=PART_Root, Path=DataContext.PathToCommand}" />
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I had a leaking handle problem (Not enough quota available to process this command.)
This problem is not readily reproducible in a simple example here but was wondering
In xp 32bit this line compiles with not problem however in vista 64bit this
This problem involved me not knowing enough of C++. I am trying to access
I had a problem with set not working in a batch file; it took
Question is similar to this (unanswered) and this one (same problem not involving Git).
I am stumped by this practice problem (not for marks): {w is an element
The one-liner should: solve a real-world problem not be extensively cryptic (should be easy
The problem is not about randomness itself (we have rand), but in cryptographically secure
The problem is not solved although I accepted one answer. Problem: Vim updates very

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.