I'm new to WPF, in the past I've used Windows Forms. I'm having an issue here that I like someone to explain to me. The below is a really simple example.
I've got a XAML page on which I've got a single checkbox, a button, and a text field. The checkbox is checked by default.
When the checkbox is unchecked, I want to enable the button and the text field, e.g.
private void UseDefaultFoldersCB_Checked(object sender, RoutedEventArgs e)
{
//MessageBox.Show("");
if (StartDirLocationTB.IsEnabled == false)
{
StartDirLocationTB.IsEnabled = true;
}
if (SelectStartLocationBtn.IsEnabled == false)
{
SelectStartLocationBtn.IsEnabled = true;
}
}
XAML:
<CheckBox Content="Use Default Folders" IsChecked="True" Height="16" HorizontalAlignment="Left" Margin="10,14,0,0" Name="UseDefaultFoldersCB" VerticalAlignment="Top" Checked="UseDefaultFoldersCB_Checked" />
<TextBox Height="23" IsEnabled="False" HorizontalAlignment="Left" Margin="9,38,0,0" Name="StartDirLocationTB" VerticalAlignment="Top" Width="403" Background="WhiteSmoke" />
<Button Content="Select Start Folder" IsEnabled="False" Height="23" HorizontalAlignment="Right" Margin="0,38,6,0" Name="SelectStartLocationBtn" VerticalAlignment="Top" Width="139" />
Stack Trace:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=TestProject StackTrace: at TestProject.MainWindow.UseDefaultFoldersCB_Checked(Object sender, RoutedEventArgs e) in C:\Users\jc\Desktop\Test\TestProject\MainWindow.xaml.cs:line 611 at System.Windows.EventRoute.InvokeHandlersImpl(Object source, RoutedEventArgs args, Boolean reRaised) at System.Windows.UIElement.RaiseEventImpl(DependencyObject sender, RoutedEventArgs args) at System.Windows.Controls.Primitives.ToggleButton.OnIsCheckedChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.FrameworkElement.OnPropertyChanged(DependencyPropertyChangedEventArgs e) at System.Windows.DependencyObject.NotifyPropertyChange(DependencyPropertyChangedEventArgs args) at System.Windows.DependencyObject.UpdateEffectiveValue(EntryIndex entryIndex, DependencyProperty dp, PropertyMetadata metadata, EffectiveValueEntry oldEntry, EffectiveValueEntry& newEntry, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType) at System.Windows.DependencyObject.SetValueCommon(DependencyProperty dp, Object value, PropertyMetadata metadata, Boolean coerceWithDeferredReference, Boolean coerceWithCurrentValue, OperationType operationType, Boolean isInternal) at System.Windows.DependencyObject.SetValue(DependencyProperty dp, Object value) at MS.Internal.Xaml.Runtime.ClrObjectRuntime.SetValue(Object inst, XamlMember property, Object value)
When I launch the application from Visual Studio, I get a NullReferenceException on the above code. Why does this code execute when the application launches? I'd have thought it would only execute when the checkbox is checked/unchecked? Why the NullReferenceException?
Thanks.