1

I had a checkbox all column inside the datagrid in WPF C#.

                     <DataGridCheckBoxColumn Binding="{Binding IsSelected,UpdateSourceTrigger=PropertyChanged}" CanUserSort="False">
                    <DataGridCheckBoxColumn.ElementStyle>
                        <Style TargetType="CheckBox" BasedOn="{StaticResource {x:Type CheckBox}}">
                            <Setter Property="VerticalAlignment" Value="Center"/>
                            <Setter Property="HorizontalAlignment" Value="Center"/>
                        </Style>
                    </DataGridCheckBoxColumn.ElementStyle>
                    <DataGridCheckBoxColumn.HeaderTemplate>
                        <DataTemplate x:Name="dtAllChkBx">
                            <CheckBox Name="cbxAll" HorizontalAlignment="Center" Margin="0,0,5,0" IsEnabled="{Binding Path=DataContext.IsCbxAllEnabled,RelativeSource={RelativeSource AncestorType=DataGrid}}"
                                      IsChecked="{Binding Path=DataContext.AllSelected,RelativeSource={RelativeSource AncestorType=DataGrid},UpdateSourceTrigger=PropertyChanged}"/>
                        </DataTemplate>
                    </DataGridCheckBoxColumn.HeaderTemplate>
                </DataGridCheckBoxColumn>

When I check the All checkbox, of course, it will mark all the checkboxes, but once I uncheck one checkbox, the All checkbox is still checked. This should be unchecked. How should I do that using WPF C#.

5
  • 6
    Can we see the code for the AllSelected property? Commented Aug 10, 2015 at 8:44
  • @GlenThomas private bool _AllSelected; public bool AllSelected { get { return _AllSelected; } set { _AllSelected = value; TaskList.ToList().ForEach(x => x.IsSelected = value); NotifyOfPropertyChange(() => AllSelected); } } Commented Aug 11, 2015 at 10:04
  • AllSelected property is here for selected all and unselecting all. Commented Aug 11, 2015 at 10:07
  • Does the IsSelected value of the TaskList items have NotifyOfPropertyChange? Commented Aug 11, 2015 at 10:55
  • writtern the full code for checkbox logic .Don't know where i am doing wrong Commented Aug 12, 2015 at 5:03

1 Answer 1

1

If I understood you correctly - after any change of IsSelected property inside collection item you should update AllSelected value.

So, you need some callback inside all your items(event or Action or any mechanism you want) and change get logic for AllSelected

Here is some draft for item IsSelected property and constructor:

public bool IsSelected {
    get { return isSelected; }
    set {
        isSelected = value;
        OnPropertyChanged();
        if (globalUpdate != null) globalUpdate();
    }
}

public ItemClass(Action globalUpdate, ...your parameters) {
    this.globalUpdate = globalUpdate;
    ...do smth with your parameters
}

Example of usage:

new ItemClass(() => OnPropertyChanged("AllSelected"))

And of course don't forget about AllSelected getter

public bool AllSelected {
        get { return YourGridItemsCollection.All(item => item.IsSelected); }

Now when you check manually all items then AllSelected will be automatically checked, and unchecked when you uncheck any item.

Sign up to request clarification or add additional context in comments.

3 Comments

And what is your problem then?
The problem is when i check AllSelected checkbox then all the checkbox in that got column got selected but if i uncheck any of the checkbox of any row in that column, The AllSelected checkbox doesnot get unchecked
The provided solution is exactly for that. You should notify AllSelected checkbox about changes inside your rows and slightly modify its getter. Please just read my answer more carefully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.