2013-03-21 93 views
0

我有下面的xml文件。我已将它复制到我的项目debug/bin文件夹&也附加到我的项目中。WPF XML DataBinding不工作

<?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中,如下所示,但没有成功。需要suggetsion,我做错了什么。

<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

你运行应用程序后你看看OUTPUT窗口吗?通常它会在那里显示绑定错误。 XML文件是嵌入式资源吗? – 2013-03-21 18:40:45

+0

如果我将“NorthwindDataSet /”加在顶部的xpath前面,我就可以使用它了。所以XPath是“/ Customers”,但我将其更改为“NorthwindDataSet/Customers”并且工作正常。 – 2013-03-21 18:57:14

+0

@Martin - 感谢您的建议。设置XPath = NorthwindDataSet/Customers为我工作。但我发现ListBox只包含一个城市=“柏林”,我期待的是城市 - “墨西哥D.F.”以及列表中。你能告诉我如何实现它吗?我尝试在列表框中设置DisplayMemberPath属性以及XPath,但它不起作用。我还有一个问题,如果我在.xml文件中设置了xmlns =“NorthwindDatSet.xsd”属性,并在项目的debug/bin文件夹中复制了.xsd文件,则绑定失败。我还将更新的.xml和.xsd文件附加到我的项目中。请建议。 – WpfBee 2013-03-22 04:05:08

回答

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> 
+0

他正在设置绑定中的SOURCE。所以即使他设置了DataContext,也不会使用DataContext,而是尝试使用Source。 – 2013-03-21 18:41:45

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>