ComboBox changed before window loads

I have a WPF / XAML window containing a ComboBox that gives me problems.

The ComboBox window disables the SelectionChanged event. The Debugger column shows me that SelectionChanged is being called (indirectly) from the Window Constructor.

The problem is that there is a Window_Loaded event in the window that does some final initialization of the data members. Since this final initialization is not yet complete, the SelectionChanged event fails with an exceptional exception.

There are several ways to solve this problem, but I would like to know the "most correct" way.

  • I can completely initialize all my data members in the constructor. This violates the concept of minimally saving constructors.

  • I could code the SelectionChanged event handler to correctly handle some data members that are null. This coding is for solving only a startup problem that will never occur after a window is completely created.

  • I could make Lazy-Loaded data members, so they are not initialized by Window_Loaded , but rather, when they are first available. There seems to be little work to solve a problem that can be solved more simply.

I guess I'm not the first person to deal with UI events before the Window Loaded event. What is the preferred way to handle this?

+4
source share
5 answers

I had a similar problem, and following it, I had a moment "ah ha." I had a default value like "IsSelected", an OnChange event, and a custom loadSettings method. At first I blamed the settings method, but it turned out that when the default value was selected, the OnChange event is fired before several controls are loaded, including the parent combo box that fires the null link. As soon as I removed "IsSelected" and allowed the default value to be null / empty, it worked fine, and my loadSettings method took care of setting the default value or the last use.

+4
source

I usually deal with the (infinitely annoying) SelectionChanged problem as follows:

 bool mySettingSelectionChangedInCode; private void SetMySettingComboBox(string value) { mySettingSelectionChangedInCode = true; mySettingComboBox.SelectedItem = value; mySettingSelectionChangedInCode = false; } private void mySettingComboBox_SelectionChanged(object sender, EventArgs args) { if (mySettingSelectionChangedInCode) return; //... } 
+3
source

This worked for me:

 if (System.Windows.Application.Current.MainWindow.IsInitialized == true) { //Do something } 
+2
source

The best way would be to create your application using the MVVM template. In this case, you do not have to solve these problems. But I understand that it’s not always possible to simply switch to MVVM if the project is not at the very beginning.

In any case, the problem you described was solved by defining a flag of type IsInitialized in your window and setting it to true after initialization is completed in the Loaded event handler. Then I would check this flag in the SelectionChanged handler, and if it is False , we will return from the method without doing anything (ignore the call).

+1
source

This can happen because you set SelectedValue from combobox after setting the ItemsSource property.

0
source

Source: https://habr.com/ru/post/1347162/


All Articles