Click here to Skip to main content
16,016,580 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

My XAML
HTML
<DataGrid x:Name="grPermission" HorizontalAlignment="Stretch"  VerticalAlignment="Stretch" IsReadOnly="True" AutoGenerateColumns="False" >
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
        </Style>
    </DataGrid.RowStyle>
    <DataGrid.Columns>
        <DataGridTemplateColumn Width="40">
            <DataGridTemplateColumn.HeaderTemplate>
                <DataTemplate>
                    <CheckBox x:Name="CheckBoxAllPermission"
                              IsChecked="{Binding IsCheckAll}"
                              Checked="CheckAllPermisson"
                              Unchecked="UnCheckAllPermisson">
                    </CheckBox>
                </DataTemplate>
            </DataGridTemplateColumn.HeaderTemplate>
            <DataGridTemplateColumn.CellTemplate>
                <DataTemplate>
                    <CheckBox x:Name="CheckBoxPermission"
                              IsChecked="{Binding Path=CheckBox,
                              UpdateSourceTrigger=PropertyChanged,
                              Mode=TwoWay}">
                    </CheckBox>
                </DataTemplate>
            </DataGridTemplateColumn.CellTemplate>
        </DataGridTemplateColumn>
        <DataGridTextColumn Header="Permission" Binding="{Binding
                            PermissionName}" Width="*" />
    </DataGrid.Columns>
</DataGrid>


Code behind
C#
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.
Posted
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:
C#
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;
        OnPropertyChanged(propertyName);

        return true;
    }

    public event PropertyChangedEventHandler? PropertyChanged;

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

And to use, inherit the ObservableObject to you ViewModel, then change your property as follows:
C#
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
 
v3

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