lundi 23 octobre 2017

ListView add checkbox to group header

I have a ListView with two groups and a few rows added two each group. I have checkboxes enabled for the listview, so every row has a checkbox. Now, I want to add checkboxes to the two groups.

I know it is possible to add checkboxes to the column header, by setting OwnerDraw = True and implementing events like DrawColumnHeader, DrawItem, DrawSubItem and ColumnClick.

But, I'm not sure how can I add checkbox to a group? I want this feature so that, if I click a particular group, all its sub-items are checked.

Can anyone help me achieve this requirement.

public void EnableColumnHeaderCheckBox(Boolean showheadercheckbox = false)
    {
        OwnerDraw = showheadercheckbox;

        if (showheadercheckbox)
            SubscribeToHeaderCheckboxEvents();
    }

    private void SubscribeToHeaderCheckboxEvents()
    {
        this.ColumnClick += new System.Windows.Forms.ColumnClickEventHandler(this.StandardListView_ColumnClick);
        this.DrawColumnHeader += new System.Windows.Forms.DrawListViewColumnHeaderEventHandler(this.StandardListView_DrawColumnHeader);
        this.DrawItem += new System.Windows.Forms.DrawListViewItemEventHandler(this.StandardListView_DrawItem);
        this.DrawSubItem += new System.Windows.Forms.DrawListViewSubItemEventHandler(this.StandardListView_DrawSubItem);
    }

    private void StandardListView_DrawSubItem(object sender, DrawListViewSubItemEventArgs e)
    {
        e.DrawDefault = true;
    }

    private void StandardListView_DrawItem(object sender, DrawListViewItemEventArgs e)
    {
        e.DrawDefault = true;
    }

    private void StandardListView_DrawColumnHeader(object sender, DrawListViewColumnHeaderEventArgs e)
    {
        if (e.ColumnIndex == 0)
        {
            Boolean value = false;

            e.DrawBackground();
            Boolean.TryParse(Convert.ToString(e.Header.Tag), out value);

            CheckBoxRenderer.DrawCheckBox(e.Graphics,
                        new Point(e.Bounds.Left + 4, e.Bounds.Top + 4),
                        new Rectangle(e.Bounds.X + 5, e.Bounds.Location.Y + 5, e.Header.Width, this.Font.Height),
                        "Col1",
                        this.Font,
                        false,
                        value ? System.Windows.Forms.VisualStyles.CheckBoxState.CheckedNormal :
            System.Windows.Forms.VisualStyles.CheckBoxState.UncheckedNormal);
        }
        else
        {
            e.DrawDefault = true;
        }
    }

    private void StandardListView_ColumnClick(object sender, ColumnClickEventArgs e)
    {
        if (this.OwnerDraw == false)
            return;

        if (e.Column == 0)
        {
            Boolean value = false;

            Boolean.TryParse(Convert.ToString(this.Columns[e.Column].Tag), out value);

            this.Columns[e.Column].Tag = !value;
            foreach (ListViewItem item in this.Items)
                item.Checked = !value;

            //this.mListView.Invalidate();
        }
    }




Aucun commentaire:

Enregistrer un commentaire