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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:36:25+00:00 2026-05-23T15:36:25+00:00

I have a WPF / XAML Window that contains a ComboBox that is giving

  • 0

I have a WPF / XAML Window that contains a ComboBox that is giving me problems.

The window’s ComboBox is firing off the SelectionChanged event.
The Debugger callstack shows me that SelectionChanged is being called (indirectly) from the Window Constructor.

The problem is that the window has an event Window_Loaded, which does some final initialization of data-members. Because this final initialization isn’t done yet, the SelectionChanged event fails with a null-reference exception.

There are several ways I could solve this, but I’d like to know the “most correct” way.

  • I could fully initialize all my data members in the constructor. This violates the concept of keeping constructors minimal.

  • I could code the SelectionChanged event handler to properly deal with some data-members being null. This is coding to deal with only a startup problem that will never occur once the Window is fully constructed.

  • I could make the data-members Lazy-Loaded, so they are not initialized by Window_Loaded, but rather when they are first accessed. Seems like a bit of work to solve a problem that could be solved more simply.

I assume I’m not the first person to deal with UI-events prior to the Window Loaded event. What is the preferred way to handle 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-23T15:36:25+00:00Added an answer on May 23, 2026 at 3:36 pm

    I had a similar problem and while tracing through it I had an “a-ha” moment. I had a default value as “IsSelected”, an OnChange event, and a custom loadSettings method. I blamed the settings method at first, but it turned out that having a default value selected triggered the OnChange event before a handful of the controls were loaded, including the parent combobox which was triggering the null reference. Once I removed the “IsSelected” and allowed the default value to be null/empty it worked fine and my loadSettings method took care of setting the default or last used value.

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

Sidebar

Related Questions

I have a grid in a WPF window, and that contains a label in
I have a WPF dll that contains a number of Window classes and exposes
I have a WPF Window that contains a UserControl with a MinWidth and MinHeight.
I am NEW to WPF. I have the following XAML code: </Window> ... <Canvas>
I have a WPF ItemsControl that is bound to an ObservableCollection. The XAML: <ItemsControl
Guys, I have a basic WPF application. Contains App.xaml as always and a Mainwindow.xaml.
I have a wpf application with a simple window that shows a text in
I have a simple WPF window that has 12 buttons on it. I want
I have a simple class that contains a string and an WPF Image control:
I have ProjectA(WPFApplication) that references ProjectB(ClassLibrary) . Inside ProjectB i have wpf window that

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.