Pergunta

I have created a data table which contains 4 columns, 3 are strings and another one is Boolean type. i mapped the datatable(xmldatadocument) to the datagridview in wpf. so the resulting looks text column and checkbox column in data grid. And i added a checkbox header template to the chekbox column header. I am able to perform uncheck all checkboxes in column when i\the header checkbox is unchecked. I am also able to perform check all checkboxes in column when the header chekcbox is checked.but i am bit troubled in if one of checkbox in that column is unchecked i need to uncheck the header checkbox also.. Kindly help me to resolve this.. i've added the screenshot, XAML and Code below.

Datagridview

PS: using WPF datagrid.

<dg:DataGrid.Columns>
                <dg:DataGridCheckBoxColumn Binding="{Binding Check}" Width="50" >
                    <dg:DataGridCheckBoxColumn.HeaderTemplate>
                        <DataTemplate x:Name="dtAllChkBx">
                            <CheckBox Name="cbxAll" Content="{x:Static properties:Resources.lblAll}" Checked="CheckBox_Checked" Unchecked="CheckBox_Unchecked"></CheckBox>
                        </DataTemplate>
                    </dg:DataGridCheckBoxColumn.HeaderTemplate>


public static ObservableCollection<Lst> list = new ObservableCollection<Lst>();
private void CheckBox_Checked(object sender, RoutedEventArgs e)
{
    for (int i = 0; i < list.Count; i++)
    {
        list[i].Check = true;
    }
}

private void CheckBox_Unchecked(object sender, RoutedEventArgs e)
{
    for (int i = 0; i < list.Count; i++)
    {
        list[i].Check = false;
    }
}

public class Lst : ObservableCollection<Lst>
{
    public bool Check { get; set; }
}  
Foi útil?

Solução

Here is the control template for the header

<Style x:Key="checkBoxHeaderStyle"
                           TargetType="{x:Type w:DataGridColumnHeader}">
                                                    <Setter Property="Template">
                            <Setter.Value>
                                <ControlTemplate TargetType="{x:Type w:DataGridColumnHeader}">
                                    <CheckBox 
                                              IsChecked="{Binding  Path = IsSelectAllChecked , Mode = TwoWay}"/>
                                </ControlTemplate>
                            </Setter.Value>
                        </Setter>
                    </Style>

Here is the template column applying the style

<w:DataGridTemplateColumn MinWidth="50"
                                              HeaderStyle="{DynamicResource checkBoxHeaderStyle}"
                                              CanUserResize="False">
                        <w:DataGridTemplateColumn.CellTemplate>
                            <DataTemplate>
                                <CheckBox Name="selectAllCheckBox"
                                                                  HorizontalAlignment="Center"                                              Margin="1,0,0,0"                                                                                            VerticalAlignment="Center"
                                          IsChecked="{Binding Path = IsSelected, Mode=TwoWay}" />
                            </DataTemplate>
                        </w:DataGridTemplateColumn.CellTemplate>
                    </w:DataGridTemplateColumn>

The is selected property is

public bool IsSelected
{
    get { return selected; }
    set { selected = value;
    OnPropertyChanged("IsSelected");
    }
}

For the header check box

public bool IsSelectAllChecked
        {
            get { return isSelectAllChecked; }
            set
            {
                isSelectAllChecked = value;
                base.OnPropertyChanged("IsSelectAllChecked");
//Call the method which sets the IsSelected property to true of false, based on value
                SetAllCheckBoxesState(value);
            }

    }

I hope it helps :)

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top