简体   繁体   English

如何从列表视图隐藏复选框

[英]How to hide checkbox from listview

I need to hide checkboxes only from specified rows. 我只需要隐藏指定行中的复选框。 Is this possible and how? 这有可能吗?

Are those rows headers? 是那些行标题吗? If so, consider using Groups in your ListView. 如果是这样,请考虑在ListView中使用

EDIT: 编辑:

I'm not aware of any way to only show checkboxes on a subset of listview items. 我不知道有什么办法只在部分listview项上显示复选框。 An alternative for you would be to set the ForeColor of the items you want to gray, and in the ItemCheck event, reset the checkstate of these items to unchecked if they are checked. 一种替代方法是设置要灰色的项目的ForeColor,然后在ItemCheck事件中将这些项目的选中状态重置为未选中(如果已选中)。

You will need to implement an owner-drawn ListBox. 您将需要实现一个所有者绘制的ListBox。 I have one which displays icons and checkboxes for all items, but you could easily modify it to only show the checkbox on specific items. 我有一个显示所有项目的图标和复选框,但是您可以轻松地对其进行修改,以仅显示特定项目上的复选框。

public class IconizedCheckedListBox : ListBox
{
    public IconizedCheckedListBox()
        : base()
    {
        DrawMode = DrawMode.OwnerDrawVariable;
        DoubleBuffered = true;
    }

    protected override void OnDrawItem(DrawItemEventArgs e)
    {
        Graphics g = e.Graphics;
        g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;


        using (SolidBrush b = new SolidBrush(BackColor))
            g.FillRectangle(b, e.Bounds);

        //e.DrawBackground();
        //e.DrawFocusRectangle();

        if (e.Index < Items.Count && e.Index != -1)
        {
            IconizedCheckedListBoxItem item = (IconizedCheckedListBoxItem)Items[e.Index];

            Rectangle checkBounds = e.Bounds;
            checkBounds.X += kCheckboxPadding;
            checkBounds.Y += (checkBounds.Height - kCheckboxSize) / 2;
            checkBounds.Width = kCheckboxSize;
            checkBounds.Height = kCheckboxSize;
            CheckBoxRenderer.DrawCheckBox(g, checkBounds.Location, 
                item.Checked?CheckBoxState.CheckedNormal:CheckBoxState.UncheckedNormal);

            using (SolidBrush b = new SolidBrush(ForeColor))
            {
                StringFormat format = new StringFormat();
                format.LineAlignment = StringAlignment.Center;
                format.Alignment = StringAlignment.Near;
                Rectangle textBounds = e.Bounds;
                textBounds.X += item.Icon.Width + 2*kCheckboxPadding + kCheckboxSize;
                textBounds.Y += 1;
                textBounds.Width -= item.Icon.Width;
                textBounds.Height -= 1;
                Font f;
                if (item.Checked)
                    f = new Font(Font, FontStyle.Bold);
                else
                    f = Font;
                g.DrawString(item.Data.ToString(), f, b, textBounds, format);
            }

            Image icon;
            if (!item.Checked)
                icon = Utilities.Utilities.WashOutImage(item.Icon);
            else
                icon = item.Icon;
            g.DrawImage(icon, e.Bounds.X + 2 * kCheckboxPadding + kCheckboxSize, e.Bounds.Y);
        }
    }

    protected override void OnMeasureItem(MeasureItemEventArgs e)
    {
        e.ItemHeight = kItemHeight;
    }

    protected override void  OnMouseClick(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            int idx = IndexFromPoint(e.Location);
            if (idx != -1)
            {
                IconizedCheckedListBoxItem item = (IconizedCheckedListBoxItem)Items[idx];
                item.Checked = !item.Checked;

                if (ItemCheck != null)
                {
                    ItemCheckEventArgs args = new ItemCheckEventArgs(
                        idx,
                        item.Checked ? CheckState.Checked : CheckState.Unchecked,
                        CheckState.Indeterminate);
                    ItemCheck(this, args);
                }
                Invalidate();
            }
        }
        base.OnMouseClick(e);
    }

    /// <summary>
    /// This is called AFTER the check state has been updated
    /// </summary>
    public event ItemCheckEventHandler ItemCheck;

    private const int kItemHeight = 32;
    private const int kCheckboxSize = 13;
    private const int kCheckboxPadding = 4;
}

public class IconizedCheckedListBoxItem
{
    public IconizedCheckedListBoxItem(Image inIcon, object inData)
    {
        Icon = inIcon;
        Data = inData;
        Checked = false;
    }

    public override string ToString()
    {
        return Data.ToString();
    }

    public Image Icon;
    public object Data;
    public bool Checked;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM