简体   繁体   English

带有复选框的.NET TreeView控件

[英].NET TreeView control with checkboxes

The TreeView control has the checkboxes property, but it puts a checkbox on every node. TreeView控件具有复选框属性,但它在每个节点上放置一个复选框。 How do I put a checkbox on just the nodes I want? 如何在我想要的节点上放置一个复选框?

Use StateImageList and TreeNode.StateImageIndex for such purposes. 为此目的使用StateImageListTreeNode.StateImageIndex You also need to subscribe to MouseDown event and change check state (state image) when user clicks on state image. 当用户单击状态图像时,您还需要订阅MouseDown事件并更改检查状态(状态图像)。 By using this approach you can also emulate three-state check boxes for example. 通过使用此方法,您还可以模拟三态复选框。

Actually internal TreeView implementation uses actually the same methodique but this is hidden from you. 实际上,内部TreeView实现实际上使用相同的methodique但这对你来说是隐藏的。

Method for creating image for ImageList based on CheckBoxState: 基于CheckBoxState为ImageList创建图像的方法:

private Image CreateCheckBoxGlyph(CheckBoxState state)
{
    Bitmap Result = new Bitmap(imlCheck.ImageSize.Width, imlCheck.ImageSize.Height);
    using (Graphics g = Graphics.FromImage(Result))
    {
        Size GlyphSize = CheckBoxRenderer.GetGlyphSize(g, state);
        CheckBoxRenderer.DrawCheckBox(g,
          new Point((Result.Width - GlyphSize.Width) / 2, (Result.Height - GlyphSize.Height) / 2), state);
    }
    return Result;
}

I gave up on TreeView because it is limited and buggy. 我放弃了TreeView,因为它是有限的和错误的。

You should be able to get this behavior out of the box using the open source TreeViewAdv 您应该能够使用开源TreeViewAdv开箱即用

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

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