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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T04:01:28+00:00 2026-05-26T04:01:28+00:00

I will explain what I mean. Let’s say I wrapped TextBox into UserControl and

  • 0

I will explain what I mean.

Let’s say I wrapped TextBox into UserControl and expose property Id
In order to bind to this property, it has to be Dependency property. Fine, here we go(notice stupid dance with OnIdChanged calls property setter so we get INotifyPropertyChanged working):

public static readonly DependencyProperty IdProperty =
            DependencyProperty.RegisterAttached("Id", typeof(string), typeof(MyTextBox), new PropertyMetadata(OnIdChanged));

public string Id
        {
            get
            {
                return (string)GetValue(IdProperty);
            }

            set
            {
                this.SetValue(IdProperty, value);
                this.OnPropertyChanged("Id");
            }
        }

private static void OnIdChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as Lookup).Id = e.NewValue as string;
        }

So, this seems like all I need. I create another User control. Drop MyTextBox on it:

    <Lookup:MyTextBox Id="{Binding Source={StaticResource DataContextProxy}, Path=DataSource.CurrentItem.DeviceId, Mode=TwoWay, NotifyOnValidationError=True}"/>

As you see – I had to use DataContextProxy. To be honest, it’s little bit of magic for me, I did it once and tried it now when regular way wasn’t binding. How should I code my user control so I can bind to it like so?

        <Lookup:MyTextBox Id="{Binding Path=CurrentItem.DeviceId, Mode=TwoWay, NotifyOnValidationError=True}"/>

This is how I can bind TextBox next to my custom one and it works as expected. What is the secret?

EDIT

Below is 2 screenshots. First one shows how what I get as a source when I bind to Lookup control (custom UserControl) – points to SELF

Second one – next field in my XAML – is regular textbox, binds to same CurrentItem but it sources from my ViewModel
enter image description here
enter image description here

EDIT 2

I figured why DataContext was pointing to UserControl itself. I figured why but do not understand why..

In my UserControl (Lookup) code behind after initializiation I set this.DataContext = this so inside control it binds to internal properties. Somehow it propogated to parent ViewModel. After I changed this code to LayoutRoot.DataContext = this – issue resolved. But I don’t understand why it behaves like this and I still can’t get good property routing through..

  • 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-26T04:01:29+00:00Added an answer on May 26, 2026 at 4:01 am

    I covered this issue in a blog post which I wrote some time ago. If you set the DataContext of the UserControl to itself, you can no longer place it within another UserControl or Window and expect it to inherit the DataContext of its parent. This means that you cannot just sit it in your view and specify bindings to your view model. The reasons for this is that you have blocked inheritence of your ViewModel DataContext. Any properties exposed by your UserControl will have their binding Source set to the UserControl.

    The solution is to set DataContext of some element within the UserControl to the UserControl itself. Most typically you would set the immediate child of the UserControl.

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

Sidebar

Related Questions

First let me explain you what I mean by a nested transaction. Example: say
This is hard to explain but I will do my best. In order to
This will be a bit difficult to explain but I will try my best.
Okay, let's see if I can explain what I mean, here's an image to
I'm not sure how to categorize this question, so let me just explain what
Can anyone explain me how this query will work and find nth highest salary
is there something like dialog's order in Android? I explain what I mean. There
I will explain problem with an example: There is two table in my database,
The question is complicated but I will explain it in details. The goal is
The question might not be clear, so i will explain further. I saw some

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.