定义视图中数据的删除规则_将更多的自定义数据存储到树视图的树节点中

定义视图中数据的删除规则

The TTreeView Delphi component displays a hierarchical list of items— tree nodes. A node is presented by node text and an optional image. Each node in a tree view is an instance of a TTreeNode class.

TTreeView Delphi组件显示项目的分层列表- 树节点 。 节点由节点文本和可选图像表示。 树视图中的每个节点都是TTreeNode类的实例。

While you can fill in the tree view with items at design time, using the TreeView Items Editor, in most cases you would fill your tree view at run time—depending what your application is about.

虽然您可以在设计时使用树视图项目编辑器在树形视图中填充项目 ,但在大多数情况下,您将在运行时填充树形视图-取决于应用程序的用途。

The TreeView Items Editor reveals there's only a handful of information you can "attach" to a node: text and a few image indexes (for the normal state, expanded, selected and alike).

TreeView Items Editor显示只有少量信息可以“附加”到节点:文本和一些图像索引(对于正常状态,展开,选定等)。

In essence, the tree view component is easy to program against. There are a couple of methods to add new nodes to the tree and set their hierarchy.

本质上,树视图组件易于编程。 有两种方法可将新节点添加到树中并设置其层次结构。

Here's how to add 10 nodes to the tree view (named "TreeView1"). Note that the Items property provides access to all nodes in the tree. The AddChild adds a new node to the tree view. The first parameter is the parent node (to build up the hierarchy) and the second parameter is the node text.

这是将10个节点添加到树视图(名为“ TreeView1”)的方法。 请注意,Items属性提供对树中所有节点的访问。 AddChild将新节点添加到树视图。 第一个参数是父节点(用于构建层次结构),第二个参数是节点文本。

The AddChild returns the newly added TTreeNode. In the above code sample, all 10 nodes are added as root nodes (have no parent node).

AddChild返回新添加的TTreeNode。 在上面的代码示例中 ,所有10个节点都被添加为根节点(没有父节点)。

In any more complex situations you would want your nodes to carry more info— preferably to have some special values (properties) that are specific to the project you are developing.

在任何更复杂的情况下,您都希望节点携带更多信息-最好具有一些特定于您正在开发的项目的特殊值(属性)。

Say you want to display customer-order-item data from your database. Each customer can have more orders and each order is made up from more items. This is a hierarchical relation one can display in a tree view:

假设您要显示数据库中的客户订单项目数据。 每个客户可以拥有更多订单,每个订单由更多项目组成。 这是一个可以在树视图中显示的层次关系:

In your database there would be more info for each order and for each item. The tree view displays the (read only) current state - and you want to see per order (or even per item) details for the selected order.

在您的数据库中,每个订单和每个项目都会有更多信息。 树状视图显示(只读)当前状态-您想查看所选订单的每个订单(甚至每个项目)详细信息。

When the user selects the node "Order_1_1" you want the order details (total sum, date, etc) to get displayed to the user.

当用户选择节点“ Order_1_1”时,您希望将订单详细信息(总计,日期等)显示给用户。

You can, at that time fetch the required data from the database, BUT you would need to know the unique identifier (let's say an integer value) of the selected order to grab the correct data.

那时,您可以从数据库中获取所需的数据,但是您需要知道所选订单的唯一标识符(比如说整数值)才能获取正确的数据。

We need a way to store this order identifier along with the node but we cannot use the Text property. The custom value we need to store in each node is an integer (just an example).

我们需要一种与节点一起存储此订单标识符的方法,但是我们不能使用Text属性。 我们需要存储在每个节点中的自定义值是一个整数(仅作为示例)。

When such a situation happens you might be tempted to look for the Tag property (many Delphi components have) but the Tag property is not exposed by the TTreeNode class.

当发生这种情况时,您可能会想寻找Tag属性(许多Delphi组件都具有),但是TTreeNode类未公开Tag属性。

将自定义数据添加到树节点:TreeNode.Data属性 ( Add Custom Data To Tree Nodes: The TreeNode.Data Property )

The Data property of a tree node allows you to associate your custom data with a tree node. Data is a pointer and can point to objects and records. The Displaying XML (RSS Feed) Data in a TreeView shows how to store a record type variable into the Data property of a tree node.

树节点的Data属性允许您将自定义数据与树节点相关联。 数据是一个指针 ,可以指向对象和记录。 在TreeView中显示XML(RSS提要)数据显示了如何将记录类型变量存储到树节点的Data属性中。

Many item-type classes expose the Data property—you can use to store any object along with the item. An example is the TListItem of a TListView component. Here's how to add objects to the Data property.

许多项目类型的类都公开了Data属性-您可以使用它与该项目一起存储任何对象。 一个示例是TListView组件的TListItem。 这是将对象添加到Data属性的方法

将自定义数据添加到树节点:TreeView.CreateNodeClass ( Add Custom Data To Tree Nodes: The TreeView.CreateNodeClass )

If you do not want to use the Data property of the TTreeNode, but rather you would like to have your own TreeNode extended with a few properties, Delphi also has a solution.

如果您不想使用TTreeNode的Data属性,而是想用几个属性扩展自己的TreeNode,Delphi也提供了一个解决方案。

Say you want to be able to do

说你想做

Here's how to extend the standard TTreeNode with a few properties of your own:

这是使用您自己的一些属性扩展标准TTreeNode的方法:

  1. Create your TMyTreeNode by extending the TTreeNode.

    通过扩展TTreeNode创建TMyTreeNode。
  2. Add it a string property MyProperty.

    添加一个字符串属性MyProperty。
  3. Handle the OnCreateNodeClass for the tree view to specify your node class should be created.

    处理树视图的OnCreateNodeClass以指定应创建的节点类。
  4. Expose something like TreeView1_SelectedNode property on the form level. This would be of type TMyTreeNode.

    在窗体级别上公开类似TreeView1_SelectedNode属性的内容。 这将是TMyTreeNode类型。
  5. Handle tree view's OnChange to write to the SelectedNode the value of the node that is selected.

    处理树视图的OnChange,将所选节点的值写入SelectedNode。
  6. Use TreeView1_Selected.myProperty to read or write new custom value.

    使用TreeView1_Selected.myProperty读取或写入新的自定义值。

Here's the full source code (TButton: "Button1" and TTreeView: "TreeView1" on a form):

这是完整的源代码(窗体上的TButton:“ Button1”和TTreeView:“ TreeView1”):

This time the Data property of the TTreeNode class is not used. Rather, you extend the TTreeNode class to have your own version of a tree node: TMyTreeNode.

这次不使用TTreeNode类的Data属性。 而是,将TTreeNode类扩展为具有自己的树节点版本:TMyTreeNode。

Using the OnCreateNodeClass event of the tree view, you create a node of your custom class instead of the standard TTreenode class.

使用树视图的OnCreateNodeClass事件,您可以创建自定义类的节点,而不是标准TTreenode类。

翻译自: https://www.thoughtco.com/store-more-custom-data-into-the-tree-node-of-a-tree-view-1058354

定义视图中数据的删除规则

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值