locked
Set Checkbox::IsChecked Programatically in C++

    Question

  • Ok, I'm an old C++/MFC, HTML/JavaScript guy and I'm playing around with metro apps at the moment. I have a simple metro application with a checkbox in my MainPage.xaml file, and all I want to do at the moment is be able to set it as checked or unchecked via C++ in my MainPage.xaml.cpp file. I've spent way too much time on this already and I'm in need of some kind of breakthrough. So...

    1. How do I do it? When I try to assign a bool value to myCheckBox->IsChecked, I get this error

      error C2664: 'Windows::UI::Xaml::Controls::Primitives::ToggleButton::IsChecked::set' : cannot convert parameter 1 from 'bool' to 'Windows::Foundation::IReference<T> ^

    2. Is it possible to bind this checkbox to a bool class member attribute in 50 lines or less? And if so, can somebody show me how it's done?

    Thanks in advance.

     

    Thursday, December 08, 2011 4:31 PM

Answers

All replies

  • 1) IsChecked is of type IReference<bool>^ and thus you'd need to box your bool to an IReference<bool>^. To do that use PropertyValue::CreateBoolean(...). That returns an Object^ which you can dynamic_cast to an IReference<bool>^.

    2) I haven't done this yet, but you can use a converter that converts between bool and IReference<bool>^.


    http://blog.voidnish.com
    Thursday, December 08, 2011 5:31 PM
  • Thursday, December 08, 2011 5:33 PM
  • Brilliant Nishant, thanks! I should have posted these questions days ago.

    Did I miss a technology along the way where I would have learned this, or is this new? If the former, what was the technology? If the latter, where could I have ascertained this information, aside from this forum?

    I will try the IValueConverter solution as soon as I can come up for some air and re-visit these metro apps. I'll let you know how that goes.

    Thanks again, Nishant.

    Friday, December 09, 2011 4:42 PM
  • Well this is a new technology/framework but it's got some similarities to WPF/Silverlight. So anyone who's played with WPF/SL may find it a little easier to adjust to the new techniques. That said, if you've never used WPF/SL then it's a good idea to just start with WinRT directly (skipping WPF and SL completely).
    http://blog.voidnish.com
    Friday, December 09, 2011 4:49 PM
  • I understand that WPF/Silverlight did not support C++, and we have a bunch of C++ code we want to reuse. And the Metro App samples provided by MS are pretty light in the C++ area. Nishant, would you be interesting in giving a training course on WinRT/XAML Metro App development in the context of C++ to my company?
    Friday, December 09, 2011 4:55 PM
  • I understand that WPF/Silverlight did not support C++, and we have a bunch of C++ code we want to reuse. And the Metro App samples provided by MS are pretty light in the C++ area. Nishant, would you be interesting in giving a training course on WinRT/XAML Metro App development in the context of C++ to my company?


    I don't normally do training, but someone I know pretty well has an online training course available on this exact topic on PluralSight.

    See http://www.pluralsight-training.net/microsoft/Courses/TableOfContents?courseName=win8-cpp

    The course is titled "Building Windows 8 Metro Apps with C++ and XAML" and the trainer Kate Gregory is one of the most respected C++ developers/authors in North America.


    http://blog.voidnish.com
    Friday, December 09, 2011 5:11 PM
  • That's too bad. Yeah, I saw that course at PluralSight and forwarded the info I found to the decision makers for evaluation. And now I just forwarded your recommendation to them as well. We'll see where it goes from here. Thanks.
    Friday, December 09, 2011 5:19 PM
  • I had the same problem, but I also found an other solution maybe its helping someone else:
    (I know its a old thread but it was also the only one I could find regarding this problem)

    It's really simple:

    bool setcheckbox =true;
    Platform::Boolean temp = safe_cast<bool>(setcheckbox);
    checkBoxSentenceEnd->IsChecked = temp;

    Sunday, June 02, 2013 9:51 PM