Set the text of telerikPrimitives:RadCheckBox

1 Answer 246 Views
CheckBox
Daniel
Top achievements
Rank 1
Silver
Bronze
Daniel asked on 15 Mar 2022, 01:01 PM | edited on 15 Mar 2022, 01:13 PM

Hi,

1.  How do I set the text of telerikPrimitives:RadCheckBox?

2,. Documents: https://docs.telerik.com/devtools/maui/controls/checkbox/checkbox-overview

 

  <telerikPrimitives:RadCheckBox x:Name="checkboxSunday"  IsChecked="{Binding Sunday, Mode=TwoWay}"/>

3. On window it checked only after two clicks on checking why ?

4.How I style the background for all checkbock in app xaml?

1 Answer, 1 is accepted

Sort by
0
Lance | Manager Technical Support
Telerik team
answered on 16 Mar 2022, 04:29 PM

Hello Daniel,

Let me get right to your answers:

1. There is no Text property, you can add a Label next to the Checkbox if you need it

2. You did not ask a question here, you just linked to a documentation article?

3. If you have set the Indeterminate state, then the CheckBox has three states and may need an additional click to leave Indeterminate state > unchecked > checked

4. Define a style  or color in App.xaml and use on all your checkboxes

This is not a Telerik-specific topic, you can learn more on how to use XAML here visit the Microsoft documentation here => Theme an app - .NET MAUI | Microsoft Docs

Here's a small visual example of how you use a StaticResource in a XAML application

In App.xaml's Resources

<Color x:Key="MyCheckboxBackgroundColor">Red</Color>

In your other pages:

<telerikPrimitives:RadCheckBox CheckedSymbolColor="{StaticResource MyCheckBoxBackgroundColor}" />


 

 

Regards,
Lance | Manager Technical Support
Progress Telerik

Virtual Classroom, the free self-paced technical training that gets you up to speed with Telerik and Kendo UI products quickly just got a fresh new look + new and improved content including a brand new Blazor course! Check it out at https://learn.telerik.com/.

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 16 Mar 2022, 04:49 PM

I didn't set the Indeterminate state, then only after two clicks it checked why?

Lance | Manager Technical Support
Telerik team
commented on 16 Mar 2022, 05:21 PM

Please read the documentation, Daniel .NET MAUI CheckBox Documentation | Checked States | Telerik UI for .NET MAUI. You'll see that the square symbol means the IsChecked value was set to null.

Your bound boolean property is either a nullable bool, or the data binding is broken (.NET6 projects use nullable by default) and the RadCheckBox is using it's internal value instead.

Fixing all the Problems

This also sounds a lot like the other problems you've been having with your data binding.

I strongly recommend you stop everything else you're doing and implement INotifyPropertyChanged in your viewmodels and use OnPropertychanged for any property you're using for data binding. This will fix 90% of all the issues I've seen you open posts for.

INotifyPropertyChanged is a critical feature to understand when building XAML applications. It's the same in WPF, UWP, Xamarin.Forms or .NET MAUI... it's a fundamental feature that is needed in order for data bindings to get the new value that was set.

Check out this chapter from the Xamarin.Forms book on Data Binding (the same applies to MAUI, you can even use the same code) Summary of Chapter 16. Data binding - Xamarin | Microsoft Docs

Here is a head start to get your current view models fixed for now, but you'll want to know why you've been having issues with your MVVM values.

using System.ComponentModel;
using System.Runtime.CompilerServices;


public class MyViewModel : INotifyPropertyChanged
{

    // put this at the bottom of the class
    #region INotifyPropertychanged Implementation

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

 

Now you can use OnPropertychanged for any data-bound properties

public class MyViewModel : INotifyPropertyChanged
{
    private bool sunday;

    public MyViewModel()
    {
        
    }

    public bool Sunday
    {
        get => sunday;
        set
        {
            // If the new value is the same as the old value, just return (there's no need to invoke a property changed)
            if (sunday != value) 
                return;
            
            // If the new value is different, update the backing field
            sunday = value;

            // Then call OnPropertyChanged (this is what updates all your data bindings)
            OnPropertyChanged();
        }
    }

    #region INotifyPropertychanged Implementation

    public event PropertyChangedEventHandler PropertyChanged;

    protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
    {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }

    #endregion
}

Daniel
Top achievements
Rank 1
Silver
Bronze
commented on 16 Mar 2022, 06:52 PM

why sunday is not nullable ?
Tags
CheckBox
Asked by
Daniel
Top achievements
Rank 1
Silver
Bronze
Answers by
Lance | Manager Technical Support
Telerik team
Share this question
or