程序功能:
使用ComboBox做级联,数据源为XML文件,适合小数据量呈现
程序代码:
<Window x:Class="WpfApplication1.LayouTest" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:core="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:WpfApplication1" Title="LayouTest" Height="628.125" Width="596.484"> <Canvas> <Canvas.Resources> <!--Create list of enumeration values--> <XmlDataProvider x:Key="xml" XPath="/countries/country" Source="ParamConfig.xml"> </XmlDataProvider> </Canvas.Resources> <Label Content="XML" Canvas.Left="15" Canvas.Top="128"/> <ComboBox Name="cmboxCountry" Canvas.Left="68" Canvas.Top="128" Width="120" ItemsSource="{Binding Source={StaticResource xml}}" DisplayMemberPath="@name" IsSynchronizedWithCurrentItem="True" SelectionChanged="cmboxCountry_SelectionChanged"/> <ComboBox Name="comboProvince" Canvas.Left="208" Canvas.Top="128" Width="120" DataContext="{Binding ElementName=cmboxCountry, Path=SelectedItem}" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding XPath=province}" DisplayMemberPath="@name"/> <ComboBox Canvas.Left="345" Canvas.Top="128" Width="120" DataContext="{Binding ElementName=comboProvince, Path=SelectedItem}" IsSynchronizedWithCurrentItem="True" ItemsSource="{Binding XPath=city}"/> </Canvas> </Window>
XML文件:
<?xml version="1.0" encoding="utf-8" ?> <countries xmlns=""> <country name="中国"> <province name="陕西"> <city>西安</city> <city>宝鸡</city> </province> <province name="山西"> <city>太原</city> <city>大同</city> </province> <province name="内蒙古自治区"> <city>呼和浩特</city> <city>包头</city> <city>集宁</city> </province> <province name="河北"> <city>石家庄</city> <city>保定</city> </province> </country> <country name="美国"> <province name="加利福尼亚"> <city>洛杉矶</city> <city>圣迭戈</city> </province> <province name="福罗里达"> <city>杰克逊维尔</city> <city>迈阿密</city> </province> </country> </countries>
注意要点:
使用XmlDataProvider一定要注意级联绑定数据不是Path而是XPath
参考文章:
非常感激http://www.cnblogs.com/mgen/archive/2011/06/19/2084553.html#commentform