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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T13:55:52+00:00 2026-05-29T13:55:52+00:00

I have a strongly typed View class that all my UserControls derive from. It

  • 0

I have a strongly typed View class that all my UserControls derive from. It looks more or less like this:

public class View<TContext> : UserControl 
{

    /// <summary>
        /// Gets or sets a value indicating whether to auto create the data context type.
        /// </summary>
    public static DependencyProperty AutoCreateDataContextProperty = DependencyProperty.Register("AutoCreateDataContext", typeof(bool), typeof(View<TContext>), new PropertyMetadata(false));
    /// <summary>
    /// Gets or sets a value indicating whether to auto create the data context type.
    /// </summary>
    /// <value>
    ///     <c>true</c> if [auto resolve data context]; otherwise, <c>false</c>.
    /// </value>
    public bool AutoCreateDataContext
    {
        get { return (bool)GetValue(AutoCreateDataContextProperty); }
        set { SetValue(AutoCreateDataContextProperty, value); }
    }

    /// <summary>
    /// Gets or sets the view model.
    /// </summary>
    /// <value>
    /// The view model.
    /// </value>
    public new TContext DataContext
    {
        get
        {
            if (AutoCreateDataContext && !DesignerProperties.GetIsInDesignMode(new ContentControl()))
            {
                base.DataContext = ServiceProvider.Current.GetService<TContext>();
            }
            return (TContext)base.DataContext;
        }
        set { base.DataContext = value; }
    }
}

The bit about AutoCreateDataContext is new…and is the source of my question. Adding this to the View<TContext> base class hasn’t caused any problems itself…but once I set the value to true in one of my derived Views:

<s:View x:TypeArguments="local:PersonSearchViewModel"
    x:Class="PersonSearchView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" ...
             Height="600" Width="800" Background="White" AutoCreateDataContext="True">

InitializeComponent for this view throws the following exception:

System.NullReferenceException occurred
  Message=Object reference not set to an instance of an object.
  Source=PresentationFramework
  StackTrace:
       at System.Windows.Markup.WpfXamlLoader.TransformNodes(XamlReader xamlReader, XamlObjectWriter xamlWriter, Boolean onlyLoadOneNode, Boolean skipJournaledProperties, Boolean shouldPassLineNumberInfo, IXamlLineInfo xamlLineInfo, IXamlLineInfoConsumer xamlLineInfoConsumer, XamlContextStack`1 stack, IStyleConnector styleConnector)
       at System.Windows.Markup.WpfXamlLoader.Load(XamlReader xamlReader, IXamlObjectWriterFactory writerFactory, Boolean skipJournaledProperties, Object rootObject, XamlObjectWriterSettings settings, Uri baseUri)
       at System.Windows.Markup.WpfXamlLoader.LoadBaml(XamlReader xamlReader, Boolean skipJournaledProperties, Object rootObject, XamlAccessLevel accessLevel, Uri baseUri)
       at System.Windows.Markup.XamlReader.LoadBaml(Stream stream, ParserContext parserContext, Object parent, Boolean closeStream)
       at System.Windows.Application.LoadComponent(Object component, Uri resourceLocator)
       at ....

As soon as I remove the AutoCreateDataContext=True from the markup, it works fine again. There is no inner exception or further exception detail. How can I debug/resolve this?

  • 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-29T13:55:53+00:00Added an answer on May 29, 2026 at 1:55 pm

    I did some guessing and then disassembling and found it to be a bug in how WPF handles DependencyProperties declared on generic DependencyObjects (like my View<T>).

    Made an abstract non-generic base class (called View, which View<T> now inherits from) and declared my DependencyProperties there instead. Problem solved.

    I guess I’ve gotten used to just how poor Microsoft quality is…so I’ve actually started recognizing trends in bugs like this.

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

Sidebar

Related Questions

I have a strongly typed viewpage that I created that looks like this: <%@
I have a strongly typed View that accepts a Customer model, this customer model
I have a strongly typed view inheriting from a POCO class. I want to
i have an strongly typed object that represents all of the textbox properties of
I'm wanting to have a strongly typed user control that accepts the class PaginatedList<T>
OK so I have a strongly-typed Customer Details view that takes a Customer object
I have a view model like this: public class EditVM { public Media.Domain.Entities.Movie Movie
I have a strongly-typed view that receives a Design model for its rendering. My
I have a very simple viewmodel class, and a strongly typed view (which uses
I have a strongly typed view, Edit, with a model named OrderModel. In this

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.