在运行时创建的Treeview不显示节点

时间:2015-03-12 20:55:07

标签: c# winforms treeview

我有两个treeview控件,一个在设计时手动添加,另一个在运行时创建。字符串ParentNode应显示在面板的两侧,但它不会显示在运行时创建的树视图中。我不确定它是否是基本的,我是否缺少或者自定义控件Accordion是否是问题。

为什么runTimecustomTreeView没有显示节点?

enter image description here

代码:

using ExpanderApp;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{
    public partial class Form1 : Form
    {
        private CustomTreeView runTimecustomTreeView = new CustomTreeView();

        public Form1()
        {
            InitializeComponent();
            CreateAccordion();
            BuildTreeViewTest1();
            BuildTreeViewTest2();
        }

        private void BuildTreeViewTest1()
        {
            //Control Added design time
            customTreeView1.Nodes.Add("ParentNode");
        }

        private void BuildTreeViewTest2()
        {
            //Control Added run time
            runTimecustomTreeView.Nodes.Add("ParentNode");
        }

        private void CreateAccordion()
        {
            Accordion accordion = new Accordion();
            accordion.Dock = DockStyle.Fill;
            Expander expander1 = new Expander();
            expander1.BorderStyle = BorderStyle.FixedSingle;
            ExpanderHelper.CreateLabelHeader(expander1, "Navigation", SystemColors.ActiveBorder);
            runTimecustomTreeView.Dock = DockStyle.Fill;
            expander1.Controls.Add(runTimecustomTreeView);
            accordion.Add(expander1);

            splitContainer1.Panel1.Controls.Add(accordion);
        }


        private void CreateContentLabel(Expander expander, string text, int height)
        {
            Label labelContent = new Label();
            labelContent.Text = text;
            labelContent.Size = new System.Drawing.Size(expander.Width, height);
            expander.Content = labelContent;
        }
    }


    //Treeview subclass to prevent double clicking checkboxes
    class CustomTreeView : TreeView
    {
        protected override void WndProc(ref Message m)
        {
            // Filter WM_LBUTTONDBLCLK
            if (m.Msg != 0x203) base.WndProc(ref m);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

替换:

runTimecustomTreeView.Dock = DockStyle.Fill;

使用

runTimecustomTreeView.Dock = DockStyle.Bottom;

您将立即看到错误是什么。你的手风琴控制没有考虑到它的儿童控制,只是把东西放在它们上面。