Click here to Skip to main content
15,662,507 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
I have a DataGrid with 2 column: checkbox column and Permission column. In new mode, when users select checkbox All in header then all child's checkbox is selected and vice versa (This logic is woking now). In edit mode, application will check if all permission is selected, checkbox all is also selected. Currently, I define a bool variable is IsCheckAll in code behind and set True if all permission is selected and binding this var as the following code but it's not working.

Image of my datagrid

<DataGrid x:Name="grPermission" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" IsReadOnly="True" AutoGenerateColumns="False" >
        <Style TargetType="DataGridRow">
        <DataGridTemplateColumn Width="40">
                    <CheckBox x:Name="CheckBoxAllPermission"
                              IsChecked="{Binding IsCheckAll}"
                    <CheckBox x:Name="CheckBoxPermission"
                              IsChecked="{Binding Path=CheckBox,
        <DataGridTextColumn Header="Permission" Binding="{Binding
                            PermissionName}" Width="*" />

Code behind
public bool IsCheckAll { get; set; }

// do some code
IsCheckAll = lstPermissions.Any(x => x.CheckBox == false) ? false : true;
grPermission.ItemsSource = lstPermissions;

What I have tried:

Could someone find out the solution and tell me to solve it.
Updated 16-Mar-23 23:53pm

1 solution

With data-binding, you need to implement the PropertyChangedEventHandler to notify the data-binding which property has changed.

Here is a base class implementation:
public abstract class ObservableObject : INotifyPropertyChanged
    protected bool Set<TValue>(
        ref TValue field,
        TValue newValue,
        [CallerMemberName] string? propertyName = null)
        if (EqualityComparer<TValue>.Default.Equals(field, newValue))
            return false;

        field = newValue;

        return true;

    public event PropertyChangedEventHandler? PropertyChanged;

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

And to use, inherit the ObservableObject to you ViewModel, then change your property as follows:
private bool _isCheckAll
public bool IsCheckAll
    get => _isCheckAll;
    set => Set(ref _isCheckAll, value);

Also, you don't implement the UpdateSourceTrigger like that.

Please take the time to do this tutorial: MVVM Tutorial[^] or this: Model-View-ViewModel | Microsoft Learn[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900