欢迎光临
我们一直在努力

创建可编辑的xml文档(之三)执行拖放操作-.NET教程,XML应用

建站超值云服务器,限时71元/月

执行托放操作

定义了treeview 显示得内容以后,现在你应该准备处理如何四处移动元素了,大多数得开发人员在处理拖放操作时得通用观念都是很相似得,无论使用visual c++ visual basic 或者任何一种.net 语言,所以我一直用下面的四个方法处理这个操作:

mousedown—–用户选择得内容

dragenter—用户开始拖动选中得项目

dragover —用户拖动选中得项目经过另一个项目

dragdrop—用户在某个地方放下选择得项目

执行这些方法适当得给用户针对可以和不可以处理的得操作分别给予视觉反馈,同时告诉用户他们是怎样被执行的,并且不用管给定的上下文的细节操作,所以就有三个直接的问题需要被考虑:

1. 你如何使treeview 控件中的一个节点和底层xml文档中的节点进行匹配

2. 为了物理节点能够跟随图形进行转换,用户如何操作xml文档

3. 你如何有效地执行大的xml文档。如果这样的转变要不得不加强时,你不想把没有必要的东西绑定到用户界面

清单1

a treenodes position maps to an xml node using an xpath query.

private sub xmltreeview_mousedown(byval sender as object, byval e as _

system.windows.forms.mouseeventargs) handles mybase.mousedown

first check whether weve clicked on a node in the tree view; if not,

just return

dim pt as new point(e.x, e.y)

drag_node = me.getnodeat(pt)

if drag_node is nothing then return

highlight the node and build an xpath query so that we can remove it later

xpath_remove_query = buildxpathquery(drag_node)

me.selectednode = drag_node

decide whether were going to perform an intra-folder rearrangement (right

mouse button) or a genuine drag-and-drop (left mouse button);

we do this in the mousedown rather than dragenter method, since by the time

dragenter fires, the mouse may well have been dragged to a different node

if e.button = system.windows.forms.mousebuttons.right then

right_mouse_drag = true

else

right_mouse_drag = false

end if

end sub

private function buildxpathquery(byval node as system.windows.forms.treenode) as string

dim query as string = ""

do

query = "*[" & xpath_filter & "][" & (node.index + 1) & "]/" & query

node = node.parent

loop while not (node is nothing)

return query.substring(0, query.length – 1)

end function

显示了mousedown 句柄 和它调用的帮助方法buildxpathquery,首先代码检查一个被选中的节点,接着通过使用事先定义好的筛选, 存储treenode (drag_node) 和使它关联到xml文档根节点的xpath 查询(xpath_remove_query)。 例如下面的查询确定了树的根节点的第二个孩子有五个孩子文件夹,一个文件夹可以用查询"attribute::id." 唯一确定

*[attribute::id][2]/*[attribute::id][5]

当用户拖动一个节点到另外一个位置时,代码列表1提供了移动treenode 和treenode相关联的xmlnode的足够信息。你也许认为你能够得到相同的效果,而完全没有必要引用筛选,并且简单的指定像“托动文档根节点的第二个孩子到第一个孩子节点内部”这样的事情,但是这里不是你认为的那样,应该是筛选器强迫treeview 的节点层次和xml文档一一对应的,没有了它 ,这样的直接使用可能是不明智的,例如假设筛选器匹配下面的结构:

<contact>

<email />

<city />

<country />

</contact>

这样的约束意味着xpath 筛选器将contacts.xml的层次作为一个简单的子元素列表看待

[0] <contacts>

[0] <contact="alex">

[1] <contact="rebekah">

[2] <contact="justin">

然而,treeview 将相同的文档看作一个节点的层次列表

[0] <contacts>

[0] <contact="alex">

[0] <email>

[1] <city>

[2] <country>

[1] <contact="rebekah">

只要联系点从不和另一个联系点嵌套,你就能保持treeview 和 xml文档保持同步而没有必要求助于筛选器,例如 如果你想交换"alex"和"rebekah"联系点入口,你可以很容易的这么做:

指令: 移除 node[0], child[0];在node[0], child[0]之后重新插入它

treeview: 移除叫做"alex"的"contact"节点,在叫做"rebekah" 的"contact"节点之后从新插入它

xml文档:移除叫做"alex"的"contact"节点,在叫做"rebekah" 的"contact"节点之后从新插入

但是嵌套的contacts,相同的指令会引起treeview表示和xml文档表示对不准。例如 假设你试图移动在下面treeview表示中嵌套的"rebekah":

[0] <contacts>

[0] <contact="alex">

[0] <contact="rebekah">

[1] <contact="justin">

在用不同方法表现节点的xml文档中

[0] <contacts>

[0] <contact="alex">

[0] <contact="rebekah">

[1] <contact="justin">

一个对treeview 表现真正有意义的指令没有必要和xml文档执行相通的工作:

指令:remove node[0], child[0], child[0]

treeview: remove "contact" node called "rebekah"

xml文档:从一个叫做“alex”的节点上错误的移动了“email”节点

我们可以借助一个筛选器,筛选器应该能够用离散的实体区分contacts,而不是通过简单的树节点的路径进行区分。这样你就没有必要在担心如何contact "rebekah"放到它的父节点”alex”内部的正确位置了,因此你就可以保证自己的安全设置

假设一个用户决定要拖动其中一个contact,下一步就是对用户操作的内容给予反馈,一个dragenter检测操作被拖动的项目是一个treeview 节点,然后记录发生的拖拉操作。对于一个想要执行它自己的应用程序来说这个控制又很大的用处。因此变量drag_drop_active作为dragdropactive的属性直接公开

[c#]

private void xmltreeview_dragenter(object sender, system.windows.forms.drageventargs e)

{

// allow the user to drag tree nodes within a

// tree

if (e.data.getdatapresent(

"system.windows.forms.treenode", true ))

{

e.effect = dragdropeffects.move;

drag_drop_active = true;

}

else

e.effect = dragdropeffects.none;

}

[vb]

private sub xmltreeview_dragenter( _

byval sender as object, _

byval e as system.windows.forms.drageventargs) _

handles mybase.dragenter

allow the user to drag tree nodes within a tree

if e.data.getdatapresent( _

"system.windows.forms.treenode", true) then

e.effect = dragdropeffects.move

drag_drop_active = true

else

e.effect = dragdropeffects.none

end if

end sub

当用户拖动文件夹时dragover被不断的调用

private sub xmltreeview_dragover(byval sender as object, byval e as

system.windows.forms.drageventargs) handles mybase.dragover

fired continuously while a tree node is dragged. we need to override this to

provide appropriate feedback

if e.data.getdatapresent("system.windows.forms.treenode", true) then

determine which node we are dragging over

dim pt as point = me.pointtoclient(new point(e.x, e.y))

dim drop_node as treenode = me.getnodeat(pt)

if its the same as the one we last dragged over, take no further action

if drop_node is last_drop_node then

return

end if

otherwise highlight the node as a potential drop target

me.selectednode = drop_node

last_drop_node = drop_node

if the drop node and drag node are the same, indicate that the drag is

disallowed and take no further action (as per explorer)

if drag_node is drop_node then

e.effect = dragdropeffects.none

return

end if

if right_mouse_drag then

right mouse drag-and-drop operations constitute intra-folder

rearrangements which provide continuous graphical feedback

we need to cache the drop nodes parent, since it will

be inaccessible if we remove it from the tree

dim drop_parent as treenode = drop_node.parent

check if its at the same level as the node being dragged

if drag_node.parent is drop_parent then

temporarily remove the drop nodes siblings from the tree; then add

them back in a different order

dim siblings(drop_parent.nodes.count) as system.windows.forms.treenode

dim count as integer = siblings.length – 1

dim item as integer

for item = 0 to count – 1

siblings(item) = drop_parent.nodes(0)

drop_parent.nodes(0).remove()

next

for item = 0 to count – 1

if siblings(item) is drop_node then

drop_parent.nodes.add(drag_node)

else

if siblings(item) is drag_node then

drop_parent.nodes.add(drop_node)

else

drop_parent.nodes.add(siblings(item))

end if

end if

next

highlight the new node

last_drop_node = drag_node

e.effect = dragdropeffects.move

me.selectednode = drag_node

else

e.effect = dragdropeffects.none

end if

else

if the user is left-button dragging, disallow (pointless) attempts

to drag a node into its parents folder (as per explorer)

if drag_node.parent is drop_node then

e.effect = dragdropeffects.none

else

e.effect = dragdropeffects.move

end if

end if

end if

end sub

出于执行效率的原因,代码首先检查自从上次调用dragover 以后被拖动的文件是否发生了变化,如果发生了变化,代码接着判断处理中的拖动类型。以前我必须允许用户最后可以重新排序和设置层次,我在这里选择类似windows的行为(只要它被定义),在其他的地方使用我的方案。因此让用户使用左键复制或者移动文件夹是很不自然的,我们应该让用户使用右键进行处理文件夹的操作。然而这样做会产生一个小问题,因为这两个拖动将会用不同的方法进行处理:左键的拖动直到拖动结束时,而右键拖动将不断的反馈状态,即使不断的拖动,直到用户松开鼠标的按键前,文档不发生物理位置上的改变

既然这样,代码检查被拖动的节点是否是节点的兄弟节点,如果是的话,父节点的所有子节点被从树中分离出来,然后进行拖放操作交换节点位置,然后再把这些子节点添加回去。结果是:释放操作完成时,底层数据源根据当前的可视化表达方式进行更新,隐藏的底层数据和数据的可视化表达就可以保持同步。更好的处理方法是:不断的显示更新操作,因此用户可以立刻得到关于拖动的反馈,xml文档只需在拖动完成时更新一次。鼠标左键的拖放操作不需要特殊的代码,drag/drop api 可以适当的处理反馈.

用户通过松开鼠标键来完成拖放操作, 参考下面的代码列表3

listing 3. xmltreeview_dragdrop and helper methods:

the xmltreeview_dragdrop and its helper swapxmldocumentnodes methods provide logic to decide where a node belongs among its siblings

private sub xmltreeview_dragdrop(byval sender as object, byval e as _

system.windows.forms.drageventargs) handles mybase.dragdrop

cancel drag/drop

drag_drop_active = false

check that we are dropping nodes within the same tree view

if e.data.getdatapresent("system.windows.forms.treenode", true) = false then

return

end if

if its a right-mouse drag-and-drop operation, the tree view will already

show the updated hierarchy; so its just a matter of updating the xml

document to match the tree view

if right_mouse_drag then

swapxmldocumentnodes()

drag_node = nothing

last_drop_node = drag_node

xpath_remove_query = ""

else

determine which node we are dropping onto

dim pt as point = me.pointtoclient(new point(e.x, e.y))

dim drop_node as treenode = me.getnodeat(pt)

do nothing if the drag and drop target are the same node

if drag_node is drop_node then

return

end if

if drop_node is nothing then

dont allow the user to drag nodes off the tree. though the tree view

wouldnt complain, any attempt to create an xml document with 2 roots

would cause problems

return

end if

add the new node where it was dropped

drag_node.remove()

drop_node.nodes.add(drag_node)

and update the xml document to match the new tree view hierarchy

swapxmldocumentnodes()

drag_node = nothing

last_drop_node = drag_node

xpath_remove_query = ""

end if

end sub

private sub swapxmldocumentnodes()

this method updates the xml document bound to the tree view so that the two node

hierarchies are the same; it determines appropriate xpath queries to remove and

reinsert the node in question by comparing the tree views structure before and

after the drag/drop operation took place

dim node as system.xml.xmlnode

node = xml_document.documentelement.selectsinglenode(xpath_remove_query)

node.parentnode.removechild(node)

create a query to determine where the node should be reinserted

dim xpath_insert_query as string = buildxpathquery(drag_node)

we are only interested in the parent portion of the insert query

xpath_insert_query = xpath_insert_query.substring(0, xpath_insert_query.lastindexof("/"))

dim insert_parent as system.xml.xmlnode = xml_document.documentelement.selectsinglenode(xpath_insert_query)

if drag_node.parent.nodes.count = 1 then

special case: if as a result of the drag/drop operation some parent without

previous children gained a child, just add the child to the parent.

insert_parent.appendchild(node)

else

otherwise we need to insert the child at its appropriate position; xmlnode

does not have an index property, so we need to do this by hand

dim child as integer

for child = 0 to insert_parent.childnodes.count – 1

if child = drag_node.index then

insert_parent.insertbefore(node, insert_parent.childnodes(child))

return

end if

next

if weve reached here, the node to be reinserted must be the last child

insert_parent.appendchild(node)

end if

end sub

那样的话,一些简单的代码就可以完成在文档中移除树节点和它相关的文件夹,还可以通过创建适当的xpath 查询来在新的位置上重新插入文件夹。需要特别指出的是:当用户在一个没有子节点的文件夹下面插入一个文件夹时,只能通过创建一个新的子节点来实现。

赞(0)
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com 特别注意:本站所有转载文章言论不代表本站观点! 本站所提供的图片等素材,版权归原作者所有,如需使用,请与原作者联系。未经允许不得转载:IDC资讯中心 » 创建可编辑的xml文档(之三)执行拖放操作-.NET教程,XML应用
分享到: 更多 (0)

相关推荐

  • 暂无文章