Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

slider display not updated when value is changed by another window

I have a window with 2 buttons and a slider.
button1 creates a new window and the button2 changes the slider value of the new window. But slider display not updated when button2 is clicked. What is wrong?

public Window1 newWindow1;
       
public MainWindow()
{
    InitializeComponent();
}

private void Button1_Click(object sender, RoutedEventArgs e)
{
    Window1 newWindow1 = new Window1();
    newWindow1.Show();
    newWindow1.mySlider.Value = 20;
}

private void Button2_Click(object sender, RoutedEventArgs e)
{
     newWindow1.mySlider.Value += 10;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

In Button1_Click you are creating a local instance of the window instead using the field.

Change your code like this.

private void Button1_Click(object sender, RoutedEventArgs e)
{
    newWindow1 = new Window1();
    newWindow1.Show();
    newWindow1.mySlider.Value = 20;
}

But I hardy recommend following the MVVM pattern when using WPF.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading