我有以下xml文件。我已将其复制到我的项目debug / bin文件夹&也附在我的项目上。
<?xml version="1.0" standalone="yes" ?>
- <NorthwindDataSet xmlns="http://tempuri.org/NorthwindDataSet.xsd">
- <Customers>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
<ContactName>Maria Anders</ContactName>
<ContactTitle>Sales Representative</ContactTitle>
<Address>Obere Str. 57</Address>
<City>Berlin</City>
<PostalCode>12209</PostalCode>
<Country>Germany</Country>
<Phone>030-0074321</Phone>
<Fax>030-0076545</Fax>
</Customers>
- <Customers>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<ContactName>Ana Trujillo</ContactName>
<ContactTitle>Owner</ContactTitle>
<Address>Avda. de la Constitución 2222</Address>
<City>México D.F.</City>
<PostalCode>05021</PostalCode>
<Country>Mexico</Country>
<Phone>(5) 555-4729</Phone>
<Fax>(5) 555-3745</Fax>
</Customers>
</NorthwindDataSet>
我想在我的WPF应用程序中绑定CustomerName,City等属性。我尝试将其绑定在XAML中,如下所示,但未获得成功。需要建议,我做错了。
<Window.Resources>
<XmlDataProvider x:Key="NorthData" Source="Northwind.xml" XPath="/Customers"/>
</Window.Resources>
<Grid>
<Label Content="{Binding XPath=Address,FallbackValue=BindingFailed,Source={StaticResource NorthData}}" Height="28" HorizontalAlignment="Left" Margin="118,94,0,0" Name="label1" VerticalAlignment="Top" Width="127" />
<ListBox ItemsSource="{Binding Source={StaticResource NorthData},XPath=City,FallbackValue=BindingFailed}" Height="100" HorizontalAlignment="Left" Margin="128,144,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
</Grid>
答案 0 :(得分:1)
使用你的代码我稍微修改了XPath,并删除了你没有提供的XSD的引用,并且它有效:
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Window.Resources>
<XmlDataProvider x:Key="NorthData" XPath="NorthwindDataSet/Customers">
<x:XData>
<NorthwindDataSet xmlns="">
<Customers>
<CustomerID>ALFKI</CustomerID>
<CompanyName>Alfreds Futterkiste</CompanyName>
<ContactName>Maria Anders</ContactName>
<ContactTitle>Sales Representative</ContactTitle>
<Address>Obere Str. 57</Address>
<City>Berlin</City>
<PostalCode>12209</PostalCode>
<Country>Germany</Country>
<Phone>030-0074321</Phone>
<Fax>030-0076545</Fax>
</Customers>
<Customers>
<CustomerID>ANATR</CustomerID>
<CompanyName>Ana Trujillo Emparedados y helados</CompanyName>
<ContactName>Ana Trujillo</ContactName>
<ContactTitle>Owner</ContactTitle>
<Address>Avda. de la Constitucion 2222</Address>
<City>Mexico D.F.</City>
<PostalCode>05021</PostalCode>
<Country>Mexico</Country>
<Phone>(5) 555-4729</Phone>
<Fax>(5) 555-3745</Fax>
</Customers>
</NorthwindDataSet>
</x:XData>
</XmlDataProvider>
</Window.Resources>
<Grid>
<Label Content="{Binding XPath=Address
, FallbackValue=BindingFailed
, Source={StaticResource NorthData}}"
Height="28"
HorizontalAlignment="Left"
Margin="118,94,0,0" Name="label1"
VerticalAlignment="Top" Width="127" />
<ListBox ItemsSource="{Binding Source={StaticResource NorthData}
, XPath=City
, FallbackValue=BindingFailed}"
Height="100"
HorizontalAlignment="Left"
Margin="128,144,0,0"
Name="listBox1"
VerticalAlignment="Top"
Width="120" />
</Grid>
</Window>
答案 1 :(得分:0)
您必须将DataContext设置为XML
<Window.Resources>
<XmlDataProvider x:Key="NorthData" Source="Northwind.xml" XPath="/Customers"/>
</Window.Resources>
<Grid DataContext="{StaticResource NorthData}">
<Label Content="{Binding XPath=Address,FallbackValue=BindingFailed,Source={StaticResource NorthData}}" Height="28" HorizontalAlignment="Left" Margin="118,94,0,0" Name="label1" VerticalAlignment="Top" Width="127" />
<ListBox ItemsSource="{Binding Source={StaticResource NorthData},XPath=City,FallbackValue=BindingFailed}" Height="100" HorizontalAlignment="Left" Margin="128,144,0,0" Name="listBox1" VerticalAlignment="Top" Width="120" />
</Grid>