This is a migrated thread and some comments may be shown as answers.

GridViewDataColumn CellTemplate CheckBox

1 Answer 147 Views
GridView
This is a migrated thread and some comments may be shown as answers.
ss
Top achievements
Rank 1
ss asked on 14 Nov 2019, 02:46 PM
I have the following GridViewDataColumn with the checkbox and textblock Elements in the celltemplate. If the checkbox is checked, the textblockcontent in CellTemplate should be updated. But actualy the textcontent is updated when the cell get into edit mode. How can i update the content of textblock in the view if the Checkbox is checked.  
 
 
<telerik:GridViewDataColumn DataMemberBinding="{Binding IsChecked, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" IsReadOnlyBinding="{Binding Unavailable}" EditTriggers="CellClick"  Header="Check" Width="Auto">
    <telerik:GridViewDataColumn.CellTemplate>
        <DataTemplate DataType="{x:Type views:ListItem}">
            <Grid>
                <CheckBox IsChecked="{Binding Path=IsChecked}" IsEnabled="{Binding Path=Unavailable, Converter={StaticResource InverseBooleanConverter}}" Command="{...}, Path=Data}"
                          CommandParameter="{...}">
                    <TextBlock Text="{Binding Status, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" TextAlignment="Right"></TextBlock>
                </CheckBox>
 
            </Grid>
        </DataTemplate>
    </telerik:GridViewDataColumn.CellTemplate>

1 Answer, 1 is accepted

Sort by
0
Dimitar Dinev
Telerik team
answered on 18 Nov 2019, 05:00 PM

Hi,

Thank you for the code snippet.

You can achieve the desired behavior by setting the Status property in the setter of the IsChecked property. For the changes to occur, you should implement INotifyPropertyChanged or use our ViewModelBase class. Please, take a look at the following code snippet:

public class RowInfo : ViewModelBase
{
    public string Status { get { return this.IsChecked ? "Checked" : "Unchecked"; } }

    private bool isChecked;
    public bool IsChecked
    { 
        get { return this.isChecked; }
        set
        {
            if (this.isChecked != value)
            {
                this.isChecked = value;
                this.OnPropertyChanged("IsChecked");
                this.OnPropertyChanged("Status");
            }
        }
    }
}

Another way to achieve the desired behavior is by subscribing to the Checked and Unchecked events of the CheckBox. Set the sender to be a CheckBox and then get its child TextBlock by using the ChildrenOfType<> visual tree helper. Finally, create a condition which checks whether the CheckBox is checked.

For your convenience, I've prepared a sample project which demonstrates the first approach, which we recommend using. I've also included the second solution which is commented. Please, the project and let me know if it delivered the desired result.

Regards,
Dimitar Dinev
Progress Telerik

Get quickly onboarded and successful with your Telerik and/or Kendo UI products with the Virtual Classroom free technical training, available to all active customers. Learn More.
Tags
GridView
Asked by
ss
Top achievements
Rank 1
Answers by
Dimitar Dinev
Telerik team
Share this question
or