Working with CheckBox in WPF. How do I use CheckBox to turn off music or the method in which it is started?

In my app, music plays at the end of the timer. The picture of it is at the very bottom.

They are located (there are two of them for each timer separately) in the methods:

private void MusicEndWork()
            {
                    Ew = new MediaPlayer();
                    Ew.Open(new Uri(@"C:\Users\grrek\source\repos\WpfApppp\WpfApppp\Res\EndWorkSound.wav", UriKind.Absolute));
                    Ew.Play();
            }

private void MusicEndBreak()
        {
                Eb = new MediaPlayer();
                Eb.Open(new Uri(@"C:\Users\grrek\source\repos\WpfApppp\WpfApppp\Res\EndBreakSound.wav", UriKind.Absolute));
                Eb.Play();
        }

My method of solving the problem was as follows: I set the conditions in the method module , if the variable A is true, we do the code in the method, false, we do not. By default, I set the variable A = true, and already in the events(I had four of them, for two checkboxes for each state marked\not marked) I changed it its meaning.

But something didn't work.

Here is my event:

This is already a different class, so at the very beginning of it, I create an instance of the main window:

MainWindow main = new MainWindow();

        private void CheckBoxWork(object sender, RoutedEventArgs e)
        {
            main.proverkaMusicStart = false;
        }

        private void CheckBoxEnd(object sender, RoutedEventArgs e)
        {
            main.proverkaMusicEnd = false;
        }

        private void CheckBoxWorkUN(object sender, RoutedEventArgs e)
        {

            main.proverkaMusicStart = true;
        }

        private void CheckBoxEndUN(object sender, RoutedEventArgs e)
        {
            main.proverkaMusicEnd = true;
        }
  1. The question is, is it possible to implement my task in a simpler way?

  2. Is it possible to make the disable method?

  3. Why doesn't my option work?

enter a description of the image here

Author: Grek, 2020-04-16

1 answers

1) You can do this, one handler per event Checked for each checkbox.

private void CheckBoxWork(object sender, RoutedEventArgs e)
{
    main.proverkaMusicStart = (sender as CheckBox)?.IsChecked ?? false;
}

In this case, if the checkbox is checked, it will be true if not checked, or null (this also happens), it will be false.

2) What is the disable method?

3) You don't need to create an instance of the main window (why do you need 2 instances of the window?), you can get it from the application

MainWindow main = Application.Current.MainWindow as MainWindow;
 1
Author: aepot, 2020-04-16 15:15:20