本文介绍了创建自定义的TreeView /树节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要扩展树节点类,这样我可以添加自定义属性到每个节点(看到,因为WebForms的树节点不包括标签属性)。所以这是我CustomTreeNode:

I need to extend the TreeNode class such that I can add custom properties to each node (seeing as WebForms TreeNode doesn't include the Tag property). So this is my CustomTreeNode:

公共类CustomTreeNode:树节点
    {

public class CustomTreeNode : TreeNode {

    public CustomTreeNode()
    {
    }

    public CustomTreeNode(int nodeId, string nodeType)
    {
        NodeId = nodeId;
        NodeType = nodeType;
    }

    public string NodeType { get; set; }
    public int NodeId { get; set; }
}

如果我创建了一个CustomTreeNode并将其添加到一个TreeView:

If I create a CustomTreeNode and add it to a TreeView:

CustomTreeNode节点=新CustomTreeNode(1,CustomType);结果
treeView.Nodes.Add(节点);

CustomTreeNode node = new CustomTreeNode(1, "CustomType");
treeView.Nodes.Add(node);

我会再拿到铸造例外执行以下操作:

I would then get a casting exception doing the following:

CustomTreeNode selectedNode =(CustomTreeNode)TreeView.SelectedNode;

CustomTreeNode selectedNode = (CustomTreeNode)TreeView.SelectedNode;

由于TreeView控件返回一个树节点,而不是一个CustomTreeNode。

because TreeView returns a TreeNode, not a CustomTreeNode.

我已经做了一些阅读,它看起来像我需要扩展的TreeView类,并覆盖CreateNode()方法返回CustomTreeNode,而不是树节点。所以我创造了这个:

I've done some reading, and it looks like I need to extend the TreeView class, and override the CreateNode() method to return CustomTreeNode instead of TreeNode. So I created this:

public class CustomTreeView : TreeView
{
    protected override TreeNode CreateNode()
    {
        return new CustomTreeNode();
    }
}

然而

问题是,CreateNode()不带任何参数,所以你必须有呼吁CustomTreeNode类空构造。所以,当我创造了我上面的CustomTreeNode,当我拿回来从CustomTreeView的节点ID和节点类型值已经丢失,因为空构造函数返回一个节点,没有任何价值。

The problem is however, CreateNode() doesn't take any arguments, so you have to have call the empty constructor for the CustomTreeNode class. So when I created my CustomTreeNode above, when I get it back from the CustomTreeView, the nodeId and nodeType values have been lost because the empty constructor returns a node without any values.

任何帮助非常AP preciated。

Any help much appreciated.

推荐答案

这是我想出了(专家的任何建议欢迎)。实例化 CustomTreeNode S IN背后的code和setter方法​​通过设置属性。修改你的 CustomTreeNode 类来坚持在ViewState中的值。这个节点将返回您的自定义树视图中的 CreateNode 将加载ViewState的信息。

This is what I came up with (experts, any advice welcomed). Instantiate the CustomTreeNodes in your code behind and set the properties via setters. Modify your CustomTreeNode class to persist the values in ViewState. The node returned by your custom tree view's CreateNode will load the ViewState information.

树节点类:

[DefaultProperty("Text")]
[ToolboxData("<{0}:CustomTreeNode runat=server></{0}:CustomTreeNode>")]
public class CustomTreeNode : TreeNode
{
    private const int NODE_TYPE = 1;
    private const int NODE_ID = 2;

    public string NodeType { get; set; }
    public int NodeId { get; set; }

    protected override void LoadViewState(Object savedState)
    {
        if (savedState != null)
        {
            object[] myState = (object[])savedState;
            if (myState[0] != null)
                base.LoadViewState(myState[0]);
            if (myState[NODE_TYPE] != null)
                this.NodeType = (string)myState[NODE_TYPE];
            if (myState[NODE_ID] != null)
                this.NodeId = (int)myState[NODE_ID];

        }
    }

    protected override Object SaveViewState()
    {
        object baseState = base.SaveViewState();
        object[] allStates = new object[3];
        allStates[0] = baseState;
        allStates[NODE_TYPE] = this.NodeType;
        allStates[NODE_ID] = this.NodeId;

        return allStates;
    }
}

TreeView控件类:

TreeView class:

[DefaultProperty("Text")]
[ToolboxData("<{0}:CustomTreeView runat=server></{0}:CustomTreeView>")]
public class CustomTreeView : TreeView
{
    protected override TreeNode CreateNode()
    {
        // Tree node will get its members populated with the data from VIEWSTATE
        return new CustomTreeNode();
    }
}

简单的.aspx文件(假设您的自定义控制在装配中富与命名空间酒吧中定义:

Simple .aspx file (Assuming that your custom control is defined in an assembly "Foo" and a namespace "Bar":

<%@ Register TagPrefix="customControl" Assembly="Foo" Namespace="Bar"  %>

<customControl:CustomTreeView ID="sampleTree"
    runat="server" onselectednodechanged="sampleTree_SelectedNodeChanged"></customControl:CustomTreeView>
<asp:Label ID="lblSelectedNode" runat="server" ></asp:Label>

codeBehind:

CodeBehind:

    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            PopulateTree();
        }
    }

    private void PopulateTree()
    {
        sampleTree.Nodes.Clear();
        CustomTreeNode root = new CustomTreeNode();
        root.Value = "root node";

        sampleTree.Nodes.Add(root);

        // Creating some fake nodes (you would of course be using real data)
        for (int i = 0; i < 10; i++)
        {
            CustomTreeNode child = new CustomTreeNode();
            child.NodeId = i;               // Saved in ViewState
            child.NodeType = "Type " + i;   // Saved in ViewState
            child.Value = child.NodeType;
            root.ChildNodes.Add(child);
        }
    }

    protected void sampleTree_SelectedNodeChanged(object sender, EventArgs e)
    {
        CustomTreeView cTreeView = (CustomTreeView) sender;
        lblSelectedNode.Text = ((CustomTreeNode)cTreeView.SelectedNode).NodeType;
    }

这篇关于创建自定义的TreeView /树节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 01:24