WPF/C#学习笔记.2
Xml格式文件读取与通过XmlDataProvider以及资源模板“动态”绑定到TreeView
What is XML?
XML一种树结构。
XML 文档必须包含且只能有一个根元素(RootNode)。该元素是所有其他元素的父元素。
XML 文档中的元素形成了一棵文档树。这棵树从根部开始,并扩展到树的最底端。所有元素均可拥有属性(Attribute)与子元素(ChildNode):
<root>
<child attribute.Name="attribute.Value">
<subchild> a string</subchild>
<anotherSubChild attribute.Name="attribute.Value"/>
</child>
</root>
Tips of XML
- 必须有标签的开始与关闭对 比如(<root>,</root>)和(<anotherSubchild /> ),而且“<”和“</”的右侧不能留空格(至少C#读取时会报错)。
- Attribute的值用双引号围起来 比如aName="aValue"。
XML文件实例:
<?xml version="1.0" encoding="utf-8" ?>
<AircraftData>
<F-14>
<Name>Tomcat</Name>
<Description>a supersonic, twin-engine, two-seat, variable-sweep wing fighter aircraft</Description>
<Manufacturer>Grumman Aerospace Corporation</Manufacturer>
<Cost>38million</Cost>
<GeneralCharacteristics>
<Crew>2(Pilot and Radar Intercept Officer)</Crew>
<Length>62 ft 9 in (19.1 m)</Length>
<Wingspan_Spread>64 ft (19.55 m)</Wingspan_Spread>
<Wingspan_Swept>38 ft (11.58 m)</Wingspan_Swept>
<Airfoil> NACA 64A209.65 mod root, 64A208.91 mod tip</Airfoil>
<EmptyWeight> 43,735 lb (19,838 kg)</EmptyWeight>
<LoadedWeight>61,000 lb (27,700 kg)</LoadedWeight>
<MaxTakeoffWeight> 74,350 lb (33,720 kg)</MaxTakeoffWeight>
<Powerplant>2*General Electric F110-GE-400 afterburning turbofans</Powerplant>
<DryThrust>16,610 lbf (73.9 kN) each</DryThrust>
<ThrustWithAfterburner>30,200 lbf (134 kN) each</ThrustWithAfterburner>
<MaximumFuelCapacity> 16,200 lb internal; 20,000 lb with 2x 267 gallon external tanks</MaximumFuelCapacity>
</GeneralCharacteristics>
<Performance>
<MaximumSpeed> Mach 2.34 (1,544 mph, 2,485 km/h) at high altitude</MaximumSpeed>
<CombatRadius> 500 nmi (575 mi, 926 km)</CombatRadius>
<FerryRange> 1,600 nmi (1,840 mi, 2,960 km)</FerryRange>
<ServiceCeiling> 50,000=""+ ft (15,200 m)</ServiceCeiling>
<RateOfClimb> LT45,000 ft/min (229 m/s)</RateOfClimb>
<WingLoading> 96 lb/ft2[164] (468.7 kg/m^2)</WingLoading>
<ThrustWeightRatio> 0.88</ThrustWeightRatio>
</Performance>
</F-14>
</AircraftData>
将XML通过XmlDataProvider,以及设置DataTemplate实现绑定到TreeView
MainWindow.xaml
<Window x:Class="xml2treeView.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:xml2treeView"
mc:Ignorable="d"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<!-- 数据模板 -->
<HierarchicalDataTemplate x:Key="NodeTemplate">
<TextBlock x:Name="tb"/>
<HierarchicalDataTemplate.ItemsSource>
<Binding XPath="child::node()"/>
</HierarchicalDataTemplate.ItemsSource>
<HierarchicalDataTemplate.Triggers>
<!-- 在TreeViewItem中显示Node.Content的实现方法 -->
<DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
<Setter TargetName="tb" Property="Text" Value="{Binding Path=Value}"/>
</DataTrigger>
<!-- 在TreeViewItem中显示Node.Name的实现方法 -->
<DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
<Setter TargetName="tb" Property="Text" Value="{Binding Path=Name}"/>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
<!-- 设置资源绑定的对象和默认显示 -->
<XmlDataProvider x:Key="xmlDataProvider" XPath="*">
<x:XData>
<RootNode xmlns="">
<ChildNode>
<SubChildNode>this is the 1st node</SubChildNode>
<SubChildNode>this is the 2rd node</SubChildNode>
</ChildNode>
</RootNode>
</x:XData>
</XmlDataProvider>
<!-- treeView绑定的动态目标 -->
<Style x:Key="treeView_AllExpanded" TargetType="{x:Type TreeView}">
<Style.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="True"/>
</Style>
</Style.Resources>
</Style>
<Style x:Key="treeView_AllCollapsed" TargetType="{x:Type TreeView}">
<Style.Resources>
<Style TargetType="TreeViewItem">
<Setter Property="IsExpanded" Value="False"/>
</Style>
</Style.Resources>
</Style>
</Window.Resources>
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal">
<Button x:Name="cmdLoadXml"
Content="loadXml"
Margin="3"
Padding="3"
Click="cmdLoadXml_Click"
ToolTip="Clik here to pick an XML-Document to be loaded"
/>
<Button x:Name="cmdExpandAll"
Content="Expand"
Margin="3"
Padding="3"
ToolTip="Click here to expand all TreeViewNodes"
Click="cmdExpandAll_Click"/>
<Button x:Name="cmdCollapseAll"
Content="Collapse"
Margin="3"
Padding="3"
ToolTip="Click here to collapse all TreeViewNodes"
Click="cmdCollapseAll_Click"/>
</StackPanel>
<TreeView Grid.Row="1" x:Name="treeXml"
ItemTemplate="{StaticResource NodeTemplate}"
ItemsSource="{Binding Source={StaticResource xmlDataProvider}}"
Margin="3,0,3,3"/>
</Grid>
</Window>
MainWindows.xaml.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Data;
using System.Xml;
namespace xml2treeView
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void cmdExpandAll_Click(object sender, RoutedEventArgs e)
{
this.treeXml.Style = (Style)this.FindResource("treeView_AllExpanded");
}
private void cmdCollapseAll_Click(object sender, RoutedEventArgs e)
{
this.treeXml.Style = (Style)this.FindResource("treeView_AllCollapsed");
}
private void cmdLoadXml_Click(object sender, RoutedEventArgs e)
{
try {
Microsoft.Win32.OpenFileDialog openFD = new Microsoft.Win32.OpenFileDialog();
openFD.Filter = "XML Documents (*.xml)|*.xml|All Files (*.*)|*.*";
Nullable<bool> isUserPickFile = openFD.ShowDialog(this);
if(isUserPickFile == true) {
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(openFD.FileName);
XmlDataProvider xmlDP = (XmlDataProvider)this.FindResource("xmlDataProvider");
xmlDP.Document = xmlDoc;
xmlDP.XPath = "*";
}
}
catch(Exception ex) {
MessageBox.Show(ex.Message);
}
}
}
}
网友评论