setting the value of a checkbox programmatically in c# (wpf)

后端 未结 3 1515
迷失自我
迷失自我 2021-02-14 01:41

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 check

相关标签:
3条回答
  • 2021-02-14 02:04

    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;
    
    0 讨论(0)
  • 2021-02-14 02:18

    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

    0 讨论(0)
  • 2021-02-14 02:28

    You can use the IsChecked property.

    I hope this helps. Damian

    0 讨论(0)
提交回复
热议问题