11

I'm working on a small wpf project using c#. I have 2 windows. When I go from one window to the next, i need to have some items preselected on the 2nd window. I have a checkbox that I need to set the value based on information that I pull from the registry. On the 1st window, i have a reference to the 2nd window. How can I set the checkbox to checked so that when the other window opens it's already checked?

 private void btnGoToNextWindow_Click(object sender, RoutedEventArgs e)
    {
            Window2 w2 = new Window2();

            //This doesn't work             
            w2.Checked = true;

            w2.Show();
            this.Close();
     }
Michael Wilson
  • 1,207
  • 2
  • 12
  • 16

3 Answers3

19

Using this:

        Window2 w2 = new Window2();

        //This doesn't work             
        w2.Checked = true;

You're setting the Checked property of the window not the control. It should be somehting like this:

        Window2 w2 = new Window2();        
        w2.MyCheckBox.IsChecked = true;
brendan
  • 29,308
  • 20
  • 68
  • 109
6

You can use the IsChecked property.

I hope this helps. Damian

Damian Schenkelman
  • 3,505
  • 1
  • 15
  • 19
  • 1
    Please, when an answer is useful mark it as answered. This way, other people can easily recognized it and the person who answered your questions gets credit for it. – Damian Schenkelman Sep 24 '10 at 18:31
  • this is way better answer, you should never use code behind. it's almost like hardcoding. – Kevin Aug 26 '11 at 19:12
-1

I'd say that you should move towards pushing the view model into the view via IoC or some other fashion. Tie the value to a property and let the framework make your life easier via binding, instead of having to hard code values all over the place.

http://msdn.microsoft.com/en-us/library/ms752347.aspx

Aaron McIver
  • 24,527
  • 5
  • 59
  • 88
  • Misconception. Having a variable in a view model, which gets set to a value, then gets retrieved (even through a binding) still "hard codes" it to a value, just through an elaborate maze of getters-setters. That variable still has to get set originally from somewhere - even if from another property's value. Why not set it directly to the box instead of through an over-bloated method of setting/retrieving the value to/from a model variable? DataBinding for something this simple really is over-the-top. – vapcguy Sep 30 '16 at 18:44