I'm doing some initial testing with WPF and this question popped up.
No error:
<DockPanel Margin="5">
<TextBox x:Name="myTextBox" DockPanel.Dock="Right" Width="50" />
<Slider x:Name="mySlider" Width="300" SmallChange="1" TickPlacement="BottomRight" IsSnapToTickEnabled="True" ValueChanged="mySlider_ValueChanged" Minimum="1" />
</DockPanel>
private void mySlider_ValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
myTextBox.Text = (Convert.ToInt32(mySlider.Value)).ToString();
}
On the other hand, changing the order of the controls I get:
An exception of type 'System.NullReferenceException' occurred in mySlider_error.exe but was not handled in user code
It gives error:
<DockPanel Margin="5">
<Slider x:Name="mySlider" Width="300" SmallChange="1" TickPlacement="BottomRight" IsSnapToTickEnabled="True" ValueChanged="mySlider_ValueChanged" Minimum="1" />
<TextBox x:Name="myTextBox" DockPanel.Dock="Right" Width="50" />
</DockPanel>
I know I can easily solve this scenario using, for example:
<TextBox x:Name="myTextBox" DockPanel.Dock="Right" Width="50" Text="{Binding ElementName=mySlider,Path=Value}" />
I just want to know why the exception is thrown?