Skip to main content Skip to footer

How to hide checkbox on some nodes

It is not directly possible to hide checkboxes for some nodes only. As a workaround, create and assign a CustomContentPresenter to the first column. You can add Checkbox and Text elements to the presenter and remove the Checkbox element when not required.

As an example, you can create a class as seen below to help handle the logic:

class CustomPresenter : CustomContentPresenter
{
    private CheckButtonElementEx checkboxelement;
    private TextElement textElement;
    private RowPanel rowPanel;

    public CustomPresenter()
    {
        //init node elements
        checkboxelement = new CheckButtonElementEx();
        textElement = new TextElement();
        rowPanel = new RowPanel();

        checkboxelement.Size = new System.Drawing.Size(16, 16);

        rowPanel.Children.Add(checkboxelement);
        rowPanel.Children.Add(textElement);

        Child = rowPanel;
    }

    public override void SetStyle(TreeNodeCellStyles styles)
    {
        //setting the styles for node elements
        base.SetStyle(styles);
        checkboxelement.Node = Node;
        textElement.Style = new Style
        {
            Margins = new Thickness(2),
            HorizontalAlignment = Alignment.Center,
            VerticalAlignment = Alignment.Center
        };

        checkboxelement.Style = new Style
        {
            HorizontalAlignment = Alignment.Center,
            VerticalAlignment = Alignment.Center
        };
    }

    public override void SetValue(object value)
    {
        //if tag is false, remove the checkbox
        if (Node.Tag != null)
        {
            try
            {
                if (!Convert.ToBoolean(Node.Tag))
                {
                    rowPanel.Children.Remove(checkboxelement);
                }
            }
            catch
            {
                Console.WriteLine("Only Enter boolean value in Tag");
            }
        }
        //set the text and check state
        textElement.Text = Node.GetValue().ToString();
        checkboxelement.Checked = Node.CheckState == System.Windows.Forms.CheckState.Checked;
    }

Then you can handle the node:

public class CheckButtonElementEx : C1.Framework.CheckButtonElement
{
    public C1.Win.TreeView.C1TreeNode Node;
    public CheckButtonElementEx()
    {
        CheckBoxIndex = 0;
    }

    public override Image GetCheckboxImage(CheckBoxState state)
    {
        if (this.Checked)
            return GetImage(CheckBoxState.CheckedNormal);
        else
            return GetImage(CheckBoxState.UncheckedNormal);
    }

    protected override void OnClick()
    {
        if(Node != null)
        {
            Node.Checked = this.Checked;
        }
        base.OnClick();

    }

    protected override void FinalizeUI()
    {
        Node = null;
        base.FinalizeUI();
    }
}

In the sample, to hide the checkbox of a node, you can set the Tag property of that node to false.