locked
Why does WPF MenuItem IsChecked="True" triggers Checked Event during Initialization? RRS feed

  • Question

  • Hi all.  Quick question here.  I have a MenuItem like this:

    <MenuItem Header="Sound" IsCheckable="True" Checked="SoundOn" Unchecked="SoundOff" IsChecked="True" />

    When the code behind initializes, it hits my SoundOn event handler because of IsChecked = true, and that prevents me from accessing elements in the window because they have not been initialized yet.  How do I prevent the initialization from triggering the event?

    I am new to WPF so perhaps I am missing something obvious???


    -7

    Wednesday, January 2, 2013 4:32 PM

Answers

  • You can check the IsInitialized property in the code behind. You may find you just need to add some simple checks as per below:

    public partial class InitCallsEvents : Window
    {
        private bool _isSoundOn = false;
        public InitCallsEvents()
        {
            InitializeComponent();
            //We are initialized now, check if any work needs to be done
            if (_isSoundOn)
            {
                TurnOnSound(); 
            }
        }
        private void SoundOn(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("SoundOn({0}, {1}). IsInitialized={2}", sender, e, this.IsInitialized);
            _isSoundOn = true;
            if (this.IsInitialized)
            {
                TurnOnSound();
            }
        }
        private void SoundOff(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("SoundOff({0}, {1}). IsInitialized={2}", sender, e, this.IsInitialized);
        }
        private void TurnOnSound()
        {
            Console.WriteLine("Turning sound on");
        }
    }


    Lee Campbell http://LeeCampbell.blogspot.com



    • Edited by LeeCampbell Wednesday, January 2, 2013 5:04 PM Still trying to get the formatting working
    • Proposed as answer by LeeCampbell Wednesday, January 2, 2013 5:04 PM
    • Marked as answer by smyrin Thursday, January 3, 2013 4:04 PM
    Wednesday, January 2, 2013 4:52 PM
  • Hi smyrin,

    You could register the Checked event after Initialization:

    public MainWindow()
    {
        InitializeComponent();
        menuItem.IsChecked = true;
        menuItem.Checked += new RoutedEventHandler(menuItem_Checked);
    }
    
    void menuItem_Checked(object sender, RoutedEventArgs e)
    {
        //throw new NotImplementedException();
    }

    Best regards,


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by smyrin Thursday, January 3, 2013 4:04 PM
    Thursday, January 3, 2013 5:06 AM

All replies

  • You can check the IsInitialized property in the code behind. You may find you just need to add some simple checks as per below:

    public partial class InitCallsEvents : Window
    {
        private bool _isSoundOn = false;
        public InitCallsEvents()
        {
            InitializeComponent();
            //We are initialized now, check if any work needs to be done
            if (_isSoundOn)
            {
                TurnOnSound(); 
            }
        }
        private void SoundOn(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("SoundOn({0}, {1}). IsInitialized={2}", sender, e, this.IsInitialized);
            _isSoundOn = true;
            if (this.IsInitialized)
            {
                TurnOnSound();
            }
        }
        private void SoundOff(object sender, RoutedEventArgs e)
        {
            Console.WriteLine("SoundOff({0}, {1}). IsInitialized={2}", sender, e, this.IsInitialized);
        }
        private void TurnOnSound()
        {
            Console.WriteLine("Turning sound on");
        }
    }


    Lee Campbell http://LeeCampbell.blogspot.com



    • Edited by LeeCampbell Wednesday, January 2, 2013 5:04 PM Still trying to get the formatting working
    • Proposed as answer by LeeCampbell Wednesday, January 2, 2013 5:04 PM
    • Marked as answer by smyrin Thursday, January 3, 2013 4:04 PM
    Wednesday, January 2, 2013 4:52 PM
  • Hi smyrin,

    You could register the Checked event after Initialization:

    public MainWindow()
    {
        InitializeComponent();
        menuItem.IsChecked = true;
        menuItem.Checked += new RoutedEventHandler(menuItem_Checked);
    }
    
    void menuItem_Checked(object sender, RoutedEventArgs e)
    {
        //throw new NotImplementedException();
    }

    Best regards,


    Sheldon _Xiao
    MSDN Community Support | Feedback to us
    Develop and promote your apps in Windows Store
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by smyrin Thursday, January 3, 2013 4:04 PM
    Thursday, January 3, 2013 5:06 AM
  • Both answers are great examples of how to work around the issue.  Thanks!

    -7

    Thursday, January 3, 2013 4:07 PM