在用户控件中填充Treeview [英] Populating a Treeview within a User-Control

查看:47
本文介绍了在用户控件中填充Treeview的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在用户控件中有一个Treeview.我的计划是从我的主要代码中填充树形视图.我不知道应该在用户控件中添加什么以允许我的主要代码起作用以填充树.
有没有简单的方法可以引用树(Treeview1)?

主要代码:

I have a Treeview within a User Control. My plan was to populate the treeview from my main code. I don''t know what should go into my user control to allow my main code to function to populate the tree.
Is there a simple way to reference the tree(Treeview1)?

Main code:

SoftwareTabGroup.SoftwareTabControl[] SWtab = new SoftwareTabGroup.SoftwareTabControl[10];

SWtab[MainTabIndex] = new SoftwareTabGroup.SoftwareTabControl();



我想这样访问树吗?



I want to access the tree like this?

SWtab[MainTabIndex].SoftwareTree.Nodes.Clear();
SWtab[MainTabIndex].SoftwareTree.Nodes.Add(xxxxxxxx));




我的用户控制代码:




My User Control Code:

namespace SoftwareTabGroup
{
    public partial class SoftwareTabControl : UserControl
    {
        public TreeView SoftwareTree
        {
            // what goes here???
            get
            {
              return TreeView1;
            }
            set
            {
                TreeView1 = value;
            }

        }

        public RichTextBox SoftwareComment
        {
            get
            {
                return SWComment;
            }
            set
            {
                SWComment = value;
            }
        }

        public SoftwareTabControl()
        {
            InitializeComponent();
        }
    }
}



谢谢,
Gary



Thanks,
Gary

推荐答案

是的,应该起作用:如果必须这样做,那么我将设置器设为私有:
Yes, that should work: if you must do this, then I would make the setter private:
public TreeView SoftwareTree
{
    get { return TreeView1; }
    private set { TreeView1 = value;}
}

通常,我不建议将表单组件用作属性.如果只需要允许TreeView功能的子集,那么我宁愿公开公共方法ClearTree和AddTree,并保留控件的内部和私有实现.这意味着您可以更改控件的内部工作方式,而不会影响外部任何内容.

如果确实需要公开许多TreeView函数,则公开整个树可能更可靠.我还是不太喜欢它!

Normally, I would not recommend making form components available as properties. If you only need to allow a subset of TreeView functionality, then I would prefer to expose public methods ClearTree, and AddTree instead, and keep the implementation of the control internal, and private. This means that you can change how your control works internally, without ffecting anything outside.

If you do need to expose a lot of TreeView functions, it may be more reliable to expose the whole tree. I still don''t like it much though!


这篇关于在用户控件中填充Treeview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆