I have created a custom user control (JCUserControl), and I am using it in the Main Window. And my Main Window has no codebehind.
I have this in the JCUserControl codebehind:
Private Sub ImmediateRadioButton_Checked(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ImmediateRadioButton.Checked
SomeTextBox.IsEnabled = False
End Sub
When I run it, it fails with the NullReferenceException. If I comment out the SomeTextBox.IsEnabled = false, it runs without any problems.
Can somebody help?
Edit:
Found out that I could just check if the radio buttons are loaded before doing whatever I want to do.
Private Sub ImmediateRadioButton_Checked(sender As System.Object, e As System.Windows.RoutedEventArgs) Handles ImmediateRadioButton.Checked
If ImmediateRadioButton.IsLoaded Then
SomeTextBox.IsEnabled = False
End If
End Sub
Isn’t it obvious?
SomeTextBoxisnull, this is possible because not all controls can be initialized at the same time, if theRadioButtonappears earlier in the parsing order its checked event can fire before theTextBoxexits.You should approach this more declarativly anyway, e.g. bind the
TextBox.IsEnabledto theRadioButton.IsCheckedor add a new boolean property and bind both to that.e.g. for the former: