Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
WPF XML数据绑定不起作用_Wpf_Xaml_Data Binding - Fatal编程技术网

WPF XML数据绑定不起作用

WPF XML数据绑定不起作用,wpf,xaml,data-binding,Wpf,Xaml,Data Binding,我有下面的xml文件。我已将其复制到我的project debug/bin文件夹中,并已附加到我的项目 <?xml version="1.0" standalone="yes" ?> - <NorthwindDataSet xmlns="http://tempuri.org/NorthwindDataSet.xsd"> - <Customers> <CustomerID>ALFKI</CustomerID> <Comp

我有下面的xml文件。我已将其复制到我的project 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>

- 
- 
阿尔夫斯基
阿尔弗雷德·福特基斯特
玛丽亚·安德斯
销售代表
奥贝街57号
柏林
12209
德国
030-0074321 
030-0076545 
- 
阿纳特
Ana Trujillo Emparedados和helados
安娜·特鲁希略
所有者
阿夫达。宪法法院2222
墨西哥食品公司。
05021
墨西哥
(5) 555-4729 
(5) 555-3745 
我想在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> 

您必须将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> 

使用您的代码,我稍微修改了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>

阿尔夫斯基
阿尔弗雷德·福特基斯特
玛丽亚·安德斯
销售代表
奥贝街57号
柏林
12209
德国
030-0074321
030-0076545
阿纳特
Ana Trujillo Emparedados和helados
楚吉洛
所有者
阿夫达。宪法法院2222
墨西哥。
05021
墨西哥
(5) 555-4729
(5) 555-3745

运行应用程序后是否查看了输出窗口?通常在那里会显示绑定错误。XML文件也是嵌入式资源吗?如果我在顶部的xpath前面加上前缀“NorthwindDataSet/”,它就可以工作了。所以XPath是“/Customers”,但我把它改为“NorthwindDataSet/Customers”,这就行了。@Martin-谢谢你的建议。设置XPath=NorthwindDataSet/客户为我工作。但我发现列表框中只包含一个city=“Berlin”,我希望列表中也包含一个city——“Mexico D.F.”。你能建议我如何实现它吗?我尝试在ListBox中设置DisplayMemberPath属性和XPath,但没有成功。我还有一个问题,如果我在项目的debug/bin文件夹中的.xml文件和copy.xsd文件中设置了xmlns=“NorthwindDatSet.xsd”属性,那么绑定就会失败。我还将更新的.xml和.xsd文件附加到我的项目中。请建议。他正在绑定中设置源。因此,即使他设置了DataContext,它也不会使用DataContext,而是尝试使用源。