WPF User Control’s DataContext is Null

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.

Leave a Comment