简体   繁体   中英

Attempting to change inkcanvas pen size using slider C# XAML

I have a slider with values 1-10. and an inkCanvas with default height and width of 1px. I am attempting to change the height and width based on the value of the slider. This is what I have tried but it gave me this error: " Object reference not set to an instance of an object ". What does this error mean?

在此处输入图片说明

edit: included xaml code:

在此处输入图片说明

I think you must have code inside penSizeSlider_ValueChanged to change the penSizes.SelectedValue . So you need either to just remove Value="1" part from <Slider..../> in your XAML code and set the value in code behind, as that will trigger the penSizeSlider_ValueChanged event before you have chance to initialize your penSize comboBox (as you only set Items of penSize in Window_Loaded event). Or you can initialize penSize.Items in XAML instead of in the code behind. and try again.

So you need to:

  1. remove Value="1" part from <Slider..../> in your XAML

  2. change Window_Loaded to:

     private void Window_Loaded (object sender, RoutedEventArgs e) { for (var i = 1; i <= 10; i++) { penSizes.Items.Add(i); } penSizes.SelectedIndex = 0; //set slider's value here penSizeSlider.Value=1; //...rest code }

PS: It would be better to just set a breakpoint and debug by yourself.

The error means that somewhere in your code, you're working with an object that still hasn't been initialized. Try to debug to find out which one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM