Before reading this article, I highly recommend reading the previous part of the series.
RadioButton
RadioButton is used to allow to the user select single items, whereas checkbox allows users to multiselect. Each RadioButton contains the GroupName property. if we set the same groupname, all the radio buttons belong to one groupname, and based on different names, we can create one or more groups of the Radiobuttons.
Note
RadioButton should be under any one of the groupnames. If groupname is not specified radioButton is allowed to multiselect.
RadioButton has two states: selected and cleared. In the group one radio button has selected the other radio button's state to automatically clear, IsChecked property is used to find if the item has selected or not. If the item has selected, IsChecked property is true, otherwise it's false.
Tag Property
Tag Property is used to store the custom value into the control. Instead of IsChecked property, we can use the Tag property to process the operation fast.
Let's see one example of how to use RadioButton and Tag Property. Based on radiobutton selection, Windows background color should be changed.
- #include "pch.h"
-
- using namespace winrt;
- using namespace Windows::ApplicationModel;
- using namespace Windows::ApplicationModel::Activation;
- using namespace Windows::Foundation;
- using namespace Windows::UI;
- using namespace Windows::UI::Xaml;
- using namespace Windows::UI::Xaml::Controls;
- using namespace Windows::UI::Xaml::Controls::Primitives;
- using namespace Windows::UI::Xaml::Interop;
- using namespace Windows::UI::Xaml::Media;
- using namespace Windows::UI::Xaml::Navigation;
- using namespace Windows::UI::Popups;
- using namespace Windows::Storage;
- struct App :ApplicationT<App>
- {
-
- public:
- virtual ~App() = default;
- StackPanel sPanel;
-
- IInspectable MakeString(hstring captionText)
- {
- return PropertyValue::CreateString(captionText);
- }
-
- IInspectable MakeTag(int Idx)
- {
- return PropertyValue::CreateInt32(Idx);
- }
-
- RadioButton CreateRadioBtn(hstring groupName, hstring contentName,int TagIdx)
- {
- RadioButton RButton;
- RButton.GroupName(groupName);
- RButton.Content(MakeString(contentName));
- RButton.Checked({this,&App::RdButtonClick});
- RButton.Tag(MakeTag(TagIdx));
-
- return RButton;
- }
-
- hstring GetRbButtonName(IInspectable const& sender)
- {
- RadioButton chkBox = sender.as<RadioButton>();
- IPropertyValue IValue = chkBox.Content().as<IPropertyValue>();
- return IValue.GetString();
- }
-
- int GetTagIndex(IInspectable const& sender)
- {
- RadioButton chkBox = sender.as<RadioButton>();
- IPropertyValue IValue = chkBox.Tag().as<IPropertyValue>();
- return IValue.GetInt32();
- }
-
- void RdButtonClick(IInspectable const & sender, const RoutedEventArgs &args)
- {
- auto Idx = GetTagIndex(sender);
- auto chkName = GetRbButtonName(sender);
-
- switch(Idx)
- {
- case 1:
- sPanel.Background(SolidColorBrush(Colors::Red()));
- break;
- case 2:
- sPanel.Background(SolidColorBrush(Colors::Green()));
- break;
- case 3:
- sPanel.Background(SolidColorBrush(Colors::Blue()));
- break;
- }
- }
-
- void OnLaunched(LaunchActivatedEventArgs const&)
- {
-
- auto RdBtnRed = CreateRadioBtn(L"Color", L"Red",1);
- auto RdBtnGreen = CreateRadioBtn(L"Color", L"Green",2);
- auto RdBtnBlue = CreateRadioBtn(L"Color", L"Blue",3);
-
- Thickness margin;
- margin.Bottom = 10;
- margin.Left = 10;
- margin.Right = 10;
- margin.Top = 10;
-
-
- sPanel.Margin(margin);
- sPanel.Children().Append(RdBtnRed);
- sPanel.Children().Append(RdBtnGreen);
- sPanel.Children().Append(RdBtnBlue);
-
- auto window = Window::Current();
- window.Content(sPanel);
-
- window.Activate();
- }
- };
-
-
- int __stdcall wWinMain(HINSTANCE, HINSTANCE, PWSTR, int)
- {
- Application::Start([](auto &&) {make<App>(); });
- return 0;
- }
Output
Tag is an IInspectable interface, so we have to use the PropertyValue to convert the int to IInspectable
- IInspectable MakeTag(int Idx)
- {
- return PropertyValue::CreateInt32(Idx);
- }
and as operator is use to convert the IInspectable to PropertyValue value:
- int GetTagIndex(IInspectable const& sender)
- {
- RadioButton chkBox = sender.as<RadioButton>();
- IPropertyValue IValue = chkBox.Tag().as<IPropertyValue>();
- return IValue.GetInt32();
- }
-
Conclusion
I hope you understood how to use the Radiobutton control.