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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T08:59:50+00:00 2026-05-18T08:59:50+00:00

I have successfully created a number of UserControls with various functionality setting the DataContext

  • 0

I have successfully created a number of UserControls with various functionality setting the DataContext on each control to ‘this’. I’ve always had problems when trying to split them into smaller ‘inner’ UserControls that will be more useable. The problem is this:

When the DataContext is set to ‘this’, I can bind to Properties in the code behind – they don’t have to be DependencyProperties. When binding to ordinary properties, the program execution enters the code behind, where I can do things like switch ListBox items or other things that I can’t do in xaml.

But when I create a new ‘inner’ UserControl that is hosted in the original control and move these properties to the inner control, I now have to upgrade them to be DependencyProperties, so that I can bind to them from the original control. When binding to DependencyProperties, the program execution never enters the code behind and for me, THIS is the problem. By the way, I dont set the DataContext in the inner control but instead set the Name property of the root element and use the ElementName=UserControlName syntax in the binding of inner control properties.

I am aware that I can add a ValidateValueCallbackHandler to a DependencyProperty to gain access to the code behind as the bound value changes, but it must be a static method, so I can’t access any non static members (ie. my whole class).

One example is that I have a DurationPicker control that displays TimeSegments objects in ListBoxes. A TimeSegments objects is basically an ObservableCollection of TimeSegment objects and a TimeSegment has StartDate, EndDate and Duration properties. I have a MinutesPerSegment property in the DurationPicker control that was bound to values from a ComboBox. It worked perfectly. I could change the ComboBox value and the code behind would put the correct number of TimeSegment objects with the correct new values in the ListBox.

Then I created the inner control that contains a TimeSegments object and a ListBox to display it. Now the MinutesPerSegment property had to become a DependencyProperty so that I can bind to it from the outer control and program execution never enters the code behind, so now I can’t alter the number of TimeSegmentobjects in the collection.

One other thing to note. I can successfully bind to the DependencyProperties of the inner control from the outer control – I can see the changes showing in the UI. The problem is just that I need the program execution to enter the code behind when certain bound properties are updated.

What is the correct way to do this? Many thanks.

EDIT – In the example, if I create the TimeSegments objects in the outer control and bind these to the inner control then it still works. This is because the MinutesPerSegment property in the outer control is an ordinary property… However, I want each TimeSegments object to be dealt with in its own inner TimeSegmentsRow control… and so the problem.

  • 1 1 Answer
  • 3 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-18T08:59:51+00:00Added an answer on May 18, 2026 at 8:59 am

    First, you needn’t set DataContext to this just to enable binding to properties of your custom control. That’s a bad idea in case anyone decides to set another datacontext for your control. You can use relative binding instead.

    {Binding Path=PathToProperty, 
             RelativeSource={RelativeSource AncestorType=
                             {x:Type namespaceAlias:typeOfYourControl}}}
    

    and declare namespace alias for your custom control in xaml

    xmlns:namespaceAlias="clr-namespace:YourLibraryNamespace;assembly=YourLibraryAssemblyName"
    

    Second, wpf will not use your property getter and setter to access your dependency properties, that’s why there should not be any additional logic beyond GetValue and SetValue. You can specify PropertyChangedCallback when registering your property to add some logic when property changes.

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

Sidebar

Related Questions

i have successfully created distribution profiles and installed on my xcode organiser, but this
I have successfully created a feature in sharepoint that modifies the existing edit dialog
I have successfully created a custom Facebook open graph action, and have been able
I have successfully created an application which is workable on the >3.0 devices. However,
I have successfully created an app that reads from a bundled .plist file and
I have created a REST web service and successfully used it with a client.
I have created a script on mac OS X and it successfully sent an
I have created a chat application using the Jabber Smack API. I'm successfully receiving
I have created many TestProjects with Visual-Studio-2010 Ultimate . All tests were executed successfully
I have a large number of ViewModel classes. For each of these classes, there

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.