Search code examples
xmlxamlmaui

How to make a checkbox rounded


I met some issues with radiobuttons in Maui:

https://github.com/dotnet/maui/issues/11418

https://github.com/dotnet/maui/issues/7595

So I decided use a group of checkboxes instead, I need to make a checkbox look like radiobutton, I mean to make it round. I did not find any simple solution on the net.


Solution

  • You may use .NET MAUI handler to set the corner radius for CheckBox. Take a look at the following example:

    1.Create a new control which subclass CheckBox:

    public class MyRoundedCornerCheckBox : CheckBox
    {
    }
    

    2.Consume it in xaml,

    <local:MyRoundedCornerCheckBox />
    

    3.And in code behind, you could use Handler to modify the checkbox,

        public MainPage()
        {
            InitializeComponent();
            ModifyCheckBox();
    
        }
    
        public void ModifyCheckBox()
        {
            Microsoft.Maui.Handlers.CheckBoxHandler.Mapper.AppendToMapping("MyCustomization", (handler, view) =>
            {
                if (view is MyRoundedCornerCheckBox)
                {
    
    #if WINDOWS
                    handler.PlatformView.CornerRadius = new Microsoft.UI.Xaml.CornerRadius(20);
    #endif
                }          
            });
        }
    

    For more info, you could refer to Customize controls with handlers.

    Hope it helps!