如何创建WPF应用程序以在树视图中可视化任意XML文档并编辑树视图项 [英] How to Create a WPF application to visualize a arbitrary XML document in a treeview and edit the treeview items

查看:80
本文介绍了如何创建WPF应用程序以在树视图中可视化任意XML文档并编辑树视图项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何创建WPF应用程序以在树视图中可视化任意XML文档并编辑树视图项目

How to Create a WPF application to visualize a arbitrary XML document in a treeview and edit the treeview items

推荐答案

< window x:class ="CS. XML_To_TreeView"xmlns:x ="#unknown>
xmlns ="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x ="http://schemas.microsoft.com/winfx/2006/xaml"
Title =如何将XML加载到TreeView控件中"
身高="400"
宽度="550">
< window.resources>

< hierarchicaldatatemplate x:key ="NodeTemplate">
< textblock x:name ="tb">
< hierarchicaldatatemplate.itemssource>
< binding xpath ="child :: node()">

< hierarchicaldatatemplate.triggers>
< datatrigger binding ="{Binding Path = NodeType}" value ="Text">
< setter targetname ="tb" property ="Text" value ="{Binding Path = Value}">

< datatrigger binding ="{Binding Path = NodeType}" value =元素">
< setter targetname ="tb" property ="Text" value ="{Binding Path = Name}">




< xmldataprovider x:key ="xmlDP" xpath ="*">
< x:xdata>
<库存xmlns =">
< books>
< book isbn ="0-7356-0562-9" stock ="in" number ="9">
< title>行动中的XML
< summary> XML Web技术

< book isbn ="0-7356-1370-2" stock ="in" number ="8">
< title>使用C#编程Microsoft Windows
< summary>使用.NET Framework进行C#编程

< book isbn ="0-7356-1288-9" stock ="out" number ="7">
< title>内部C#
< summary> C#语言编程

< book isbn ="0-7356-1377-X" stock ="in" number ="5">
< title> Microsoft .NET简介
<摘要> .NET技术概述

< book isbn ="0-7356-1448-2" stock ="out" number ="4">
< title> Microsoft C#语言规范
< summary> C#语言定义


< cds>
< cd stock ="in" number ="3">
< title>古典典藏
< summary>古典音乐

< cd stock ="out" number ="9">
< title>爵士收藏
< summary>爵士音乐







< style.resources>
< style targettype ="TreeViewItem">
< setter property ="IsExpanded" value ="True"/>




< style.resources>
< style targettype ="TreeViewItem">
< setter property ="IsExpanded" value ="False"/>




< grid>
< grid.rowdefinitions>
< rowdefinition height ="Auto">
< rowdefinition height ="*">
< rowdefinition height ="Auto">


Content =选择要显示的XML文档..."
保证金="5"
点击="cmdLoadXml_Click"
ToolTip =单击此处以选择要加载的XML文档."
/>
< treeview name ="tv"> Background ="LightYellow"
ItemTemplate ="{StaticResource NodeTemplate}"
ItemsSource ="{Binding Source = {StaticResource xmlDP}}"
保证金="5"
Grid.Row ="1"
/>
< grid grid.row ="2">
< grid.columndefinitions>
< columndefinition width ="Auto"> < columndefinition width ="Auto"> < columndefinition width ="Auto"> < columndefinition width ="*"> < columndefinition width ="Auto">

Margin ="5"/>
< textbox x:name ="txt">
保证金="0,5"
宽度="150"
Grid.Column ="1"/>
< uniformgrid columns ="2">
保证金="5,0"
Grid.Column ="2">

点击="cmdChangeDisplayRootNode_Click"
Margin ="3"
填充="3"
ToolTip =单击此处以使用在TB中指定的路径作为左侧显示节点."
内容="_ Go"/>

点击="cmdReset_Click"
Margin ="3"
填充="3"
ToolTip =单击此处重置根显示节点(显示全部)."
Content ="_ Reset"/>

< uniformgrid columns ="2">
保证金="5,0"
Grid.Column ="4">

Content =全部展开"
Margin ="3"
填充="3"
ToolTip =单击此处以展开所有TreeView节点"
点击="cmdExpandAll_Click"/>

Content =全部折叠"
Margin ="3"
填充="3"
ToolTip =单击此处以折叠TreeView"
点击="cmdCollapse_Click"/>



<window x:class="CS.XML_To_TreeView" xmlns:x="#unknown">
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="How to load XML into a TreeView control"
Height="400"
Width="550">
<window.resources>

<hierarchicaldatatemplate x:key="NodeTemplate">
<textblock x:name="tb">
<hierarchicaldatatemplate.itemssource>
<binding xpath="child::node()">

<hierarchicaldatatemplate.triggers>
<datatrigger binding="{Binding Path=NodeType}" value="Text">
<setter targetname="tb" property="Text" value="{Binding Path=Value}">

<datatrigger binding="{Binding Path=NodeType}" value="Element">
<setter targetname="tb" property="Text" value="{Binding Path=Name}">




<xmldataprovider x:key="xmlDP" xpath="*">
<x:xdata>
<inventory xmlns="">
<books>
<book isbn="0-7356-0562-9" stock="in" number="9">
<title>XML in Action
<summary>XML Web Technology

<book isbn="0-7356-1370-2" stock="in" number="8">
<title>Programming Microsoft Windows With C#
<summary>C# Programming using the .NET Framework

<book isbn="0-7356-1288-9" stock="out" number="7">
<title>Inside C#
<summary>C# Language Programming

<book isbn="0-7356-1377-X" stock="in" number="5">
<title>Introducing Microsoft .NET
<summary>Overview of .NET Technology

<book isbn="0-7356-1448-2" stock="out" number="4">
<title>Microsoft C# Language Specifications
<summary>The C# language definition


<cds>
<cd stock="in" number="3">
<title>Classical Collection
<summary>Classical Music

<cd stock="out" number="9">
<title>Jazz Collection
<summary>Jazz Music







<style.resources>
<style targettype="TreeViewItem">
<setter property="IsExpanded" value="True" />




<style.resources>
<style targettype="TreeViewItem">
<setter property="IsExpanded" value="False" />




<grid>
<grid.rowdefinitions>
<rowdefinition height="Auto">
<rowdefinition height="*">
<rowdefinition height="Auto">


Content="Pick the XML document to be shown ..."
Margin="5"
Click="cmdLoadXml_Click"
ToolTip="Click here to pick an XML-document to be loaded."
/>
<treeview name="tv"> Background="LightYellow"
ItemTemplate= "{StaticResource NodeTemplate}"
ItemsSource="{Binding Source={StaticResource xmlDP}}"
Margin="5"
Grid.Row="1"
/>
<grid grid.row="2">
<grid.columndefinitions>
<columndefinition width="Auto"> <columndefinition width="Auto"> <columndefinition width="Auto"> <columndefinition width="*"> <columndefinition width="Auto">

Margin="5"/>
<textbox x:name="txt">
Margin="0,5"
Width="150"
Grid.Column="1"/>
<uniformgrid columns="2">
Margin="5,0"
Grid.Column="2">

Click="cmdChangeDisplayRootNode_Click"
Margin="3"
Padding="3"
ToolTip="Click here to use the path specified in the TB to the left as the root display node."
Content="_Go"/>

Click="cmdReset_Click"
Margin="3"
Padding="3"
ToolTip="Click here to reset the root display node (show all)."
Content="_Reset"/>

<uniformgrid columns="2">
Margin="5,0"
Grid.Column="4">

Content="Expand all"
Margin="3"
Padding="3"
ToolTip="Click here to expand all TreeView nodes"
Click="cmdExpandAll_Click"/>

Content="Collapse all"
Margin="3"
Padding="3"
ToolTip="Click here to collapse the TreeView"
Click="cmdCollapse_Click"/>




这篇关于如何创建WPF应用程序以在树视图中可视化任意XML文档并编辑树视图项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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