24

I have a user control where the XAML of the control can bind to the appropriate properties from the parent's data context like normal (the data context propagates in xaml).

For example, I have a window whose DataContext I am setting to ObjectA for example. My user control within the window is then try to access the properties within the dataContext

So my window's xaml and code behind can both see a non-null DataContext.

My control that DataContext propagates to can see a non-null DataContext in the Xaml but not in the code behind.

What is the proper way of handling this?

3 Answers 3

32

failing that if you need to check whether the DataContext is being set you can use the DataContextChanged

public partial class UserControl1 : UserControl
{
    public UserControl1()
    {
        InitializeComponent();

        DataContextChanged += new DependencyPropertyChangedEventHandler(UserControl1_DataContextChanged);
    }

    void UserControl1_DataContextChanged(object sender, DependencyPropertyChangedEventArgs e)
    {
        // You can also validate the data going into the DataContext using the event args
    }
}

Note it wont enter UserControl1_DataContextChanged until DataContext is changed from null to a different value.

Not sure if this answers your question but can be quite handy to use in debugging issues.

Sign up to request clarification or add additional context in comments.

2 Comments

I had to subscribe to the DataContextChanged event before InitializeComponent so that I could handle the event the first time DataContext was set.
Also works to do: DataContextChanged += UserControl1_DataContextChanged;
17

I think you are checking the 'DataContext' in the constructor of the UserControl. It will be null at the Constructor since the user control hasnt yet created while execution is in the constructor code. But check the property at Loaded event you will see the object properly.

public partial class UserControl1
{
    public UserControl1()
    {
        this.InitializeComponent();

        //DataContext will be null here 
        this.Loaded += new RoutedEventHandler(UserControl1_Loaded);
    }

    void UserControl1_Loaded(object sender, RoutedEventArgs e)
    {
        //Check DataContext Property here - Value is not null
    }
}

2 Comments

Not sure if anything's changed since this answer - but Loaded event has a context of NULL.
In my program (.NET Framework 4.6.2), DataContext is in fact initialized after InitializeComponent executes.
14

I would check to see whether you are having a binding error at runtime. Add this namespace to your XAML:

xmlns:debug="clr-namespace:System.Diagnostics;assembly=System"

and check the debugger's Output window for relevant error messages.

Alternatively, can you show us more code?

3 Comments

Thank you... this is sweet... I wish I had found this weeks ago... lol.
Oh well. Solved my completely-unrelated-problem too :) Thanks.
Teach a man to fish... I also had no idea you could do this. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.