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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:16:18+00:00 2026-05-12T17:16:18+00:00

I have a UserControl with a DependencyProperty. I set it’s value in the host

  • 0

I have a UserControl with a DependencyProperty. I set it’s value in the host window using a data binding expression. However, it doesn’t work as expected.

Snippet from the user control’s codebehind:

public class ViewBase : UserControl
{
    public static readonly DependencyProperty ViewModelProperty
        = DependencyProperty.Register(
            "ViewModel", typeof(ViewModelBase), typeof(ViewBase));

    public ViewModelBase ViewModel
    {
        get { return GetValue(ViewModelProperty) as ViewModelBase; }
        set
        {
            SetValue(ViewModelProperty, value);
        }
    }
}

And from the XAML (note: CasingListView inherits from ViewBase):

<CasingEditor:CasingListView x:Name="_casingListView"
                             ViewModel="{Binding CasingListViewModel}" />

What happens is nothing. Specifically, the setter is never called, and the property remains null. I know the source property CasingListViewModel has a value, because I have tried to bind it to another property (DataContext), and it worked fine.

I thought a dependency property could be databound. Am I wrong?

  • 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-12T17:16:18+00:00Added an answer on May 12, 2026 at 5:16 pm

    As sometimes happens, the problem turned out to be not quite what we thought.

    I mentioned that the setter was never called. That is true. The code above is slightly trimmed to make it clearer. Unfortunately, I also trimmed away a statement in the setter, following the call to SetValue. That statement assigned the value to DataContext, something like this:

    public ViewModelBase ViewModel
    {
        get { return GetValue(ViewModelProperty) as ViewModelBase; }
        set
        {
            SetValue(ViewModelProperty, value);
            DataContext = value;
        }
    }
    

    As I have now learned from this excellent article, the setter is in fact bypassed when the property is set via databinding. The framework instead works directly against the DependencyObject. So the property was actually set, but the setter was never called (as I mentioned), and the consequence was that DataContext remained null and nothing worked.

    So: First I apologize profusely for asking an unanswerable question. Second, as a way of making up for it, I can pass on the very important piece of advice:

    Never put anything but GetValue() and SetValue() inside the property getter/setter, because they are not always called!

    Edit: Later, I’ve also discovered another problem with this approach. By setting the DataContext this way, I actually lose the original data context that supports the binding in the first place. The result is that the property is immediately reset to null. So all in all not a good approach overall.

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

Sidebar

Related Questions

I have a UserControl that I want to participate in data binding. I've set
Before I start, I have this code inside of a Custom Usercontrol: private DependencyProperty
I have a UserControl which I'm using to display a list of UIElement s.
Hi I have a mainView window which has its dataContext set to it's own
I have a WPF UserControl containing a custom DependencyProperty named MyDP. I want to
I have a ListBox where I'm using a UserControl as DataTemplate. My UserControl has
I have UserControl which contains a TextBox and a Button control. The Button opens
I have a usercontrol that has several public properties. These properties automatically show up
I have a UserControl that consists of three TextBoxes. On a form I can
I have a UserControl in my Asp.net project that has a public property. 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.