WPF/C#學(xué)習(xí)筆記.2
Xml格式文件讀取與通過(guò)XmlDataProvider以及資源模板“動(dòng)態(tài)”綁定到TreeView
What is XML诱咏?
XML一種樹結(jié)構(gòu)苔可。
XML 文檔必須包含且只能有一個(gè)根元素(RootNode)。該元素是所有其他元素的父元素袋狞。
XML 文檔中的元素形成了一棵文檔樹焚辅。這棵樹從根部開始,并擴(kuò)展到樹的最底端苟鸯。所有元素均可擁有屬性(Attribute)與子元素(ChildNode):
<root>
<child attribute.Name="attribute.Value">
<subchild> a string</subchild>
<anotherSubChild attribute.Name="attribute.Value"/>
</child>
</root>
Tips of XML
- 必須有標(biāo)簽的開始與關(guān)閉對(duì) 比如(<root>同蜻,</root>)和(<anotherSubchild /> ),而且“<”和“</”的右側(cè)不能留空格(至少C#讀取時(shí)會(huì)報(bào)錯(cuò))早处。
- Attribute的值用雙引號(hào)圍起來(lái) 比如aName="aValue"湾蔓。
XML文件實(shí)例:
<?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通過(guò)XmlDataProvider,以及設(shè)置DataTemplate實(shí)現(xiàn)綁定到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>
<!-- 數(shù)據(jù)模板 -->
<HierarchicalDataTemplate x:Key="NodeTemplate">
<TextBlock x:Name="tb"/>
<HierarchicalDataTemplate.ItemsSource>
<Binding XPath="child::node()"/>
</HierarchicalDataTemplate.ItemsSource>
<HierarchicalDataTemplate.Triggers>
<!-- 在TreeViewItem中顯示Node.Content的實(shí)現(xiàn)方法 -->
<DataTrigger Binding="{Binding Path=NodeType}" Value="Text">
<Setter TargetName="tb" Property="Text" Value="{Binding Path=Value}"/>
</DataTrigger>
<!-- 在TreeViewItem中顯示Node.Name的實(shí)現(xiàn)方法 -->
<DataTrigger Binding="{Binding Path=NodeType}" Value="Element">
<Setter TargetName="tb" Property="Text" Value="{Binding Path=Name}"/>
</DataTrigger>
</HierarchicalDataTemplate.Triggers>
</HierarchicalDataTemplate>
<!-- 設(shè)置資源綁定的對(duì)象和默認(rèn)顯示 -->
<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綁定的動(dòng)態(tài)目標(biāo) -->
<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);
}
}
}
}