CellContentTemplate and checkbox

2 posts, 0 answers
  1. Hugo
    Hugo avatar
    2 posts
    Member since:
    Dec 2018

    Posted 22 May 2019 Link to this post

    I have a datagrid in which I fill it from a web services, I'm using a CellContentTemplate to integrate a checkbox, only when I try to assign the check it does not make the change.


    <telerikGrid:RadDataGrid x:Name="RDG_Detalle" 
                                    <telerikGrid:DataGridBorderStyle BackgroundColor="#D9D9D9"/>
                                <telerikGrid:RadDataGrid.Columns >
                                    <telerikGrid:DataGridTemplateColumn HeaderText="Template Column" x:Name="pr">
                                                <!-- Your content -->
                                                <telerikPrimitives:RadCheckBox x:Name="checkbox" IsChecked="{Binding Escaneado}">


                                    <telerikGrid:DataGridNumericalColumn PropertyName="Cantidad" 

                                    <telerikGrid:DataGridTextColumn PropertyName="ProductoDescripcion" 
                                    <telerikGrid:DataGridTextColumn PropertyName="AlmacenDescripcion" 

                                    <telerikGrid:DataGridTextColumn PropertyName="UbicacionDescripcion" 

                                    <telerikGrid:DataGridNumericalColumn PropertyName="Precio" 

                                    <telerikGrid:DataGridNumericalColumn PropertyName="Importe" 

                                    <telerikGrid:DataGridNumericalColumn PropertyName="Total" 

                                    <telerikGrid:DataGridBooleanColumn PropertyName="IsEscaneado"


     public bool IsEscaneado
                get => Escaneado;
                set { Escaneado = value; OnPropertyChanged(); }

            public event PropertyChangedEventHandler PropertyChanged;

            public ICommand IsCheckedChangedCommand { get; set; }

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

  2. Lance | Manager Technical Support
    Lance | Manager Technical Support avatar
    1168 posts

    Posted 23 May 2019 Link to this post

    Hi Hugo,

    There are several problems with that code:

    1 - The Checkbox is bound to the wrong property
    2 - The BindingMode is not set to TwoWay
    3 - The backing field is incorrect and there's no equality check

    Let's take care of the most important ones first.

    The biggest problem is that the Checkbox should be bound to IsEscaneado (not Escaneado).

    In XAML, you need to set the binding mode to TwoWay if you want the UI element to also update the bound property. You can learn more in the Microsoft tutorial documentation: Data Binding Basics - The Binding Mode.

    Here's what the code will look like after fixing #1 and #2

    <telerikPrimitives:RadCheckBox  IsChecked="{Binding IsEscaneado, Mode=TwoWay}"/>

    Regarding #3, I have two more suggestions:

    - I would also add an equality check in the setter, this will prevent a lot of unnecessary property changed events form firing
    - Use a property named backing field

    That way you'll never accidentally use the wrong property because it starts with a lower-case letter. Private backing fields are just for the public property to use to hold the value and should never be used for binding.

    private bool isEscaneado;
    public bool IsEscaneado
        get => isEscaneado;
            if(isEscaneado == value)
            isEscaneado= value;

    Lance | Technical Support Engineer, Principal
    Progress Telerik
    Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
Back to Top