2014-07-26 141 views
0

我有一個像休耕一個XML文件:XAML XML數據綁定不起作用

<?xml version="1.0" encoding="utf-8" ?> 
<Course 
    Title="jQueryAjax" 
    BtnAddisionalResourcesTitle="" 
    BtnAddisionalResourcesAddress=""> 
<Topic 
    Tag="ff" 
    Title=""> 

    <Lesson 
     Tag="" 
     Title="" 
     Address="" 
    /> 

    <!--...--> 


    </Topic> 

    <!--...--> 
</Course> 

,也有像休耕

<Label 
       HorizontalAlignment="Right" 
       VerticalAlignment="Top" 
       FontSize="16pt" 
       FontFamily="Myriad Pro" 
       > 
      <Label.Content> 
       <Binding Source="{StaticResource CourseInfo}" 
         XPath="Title" 
         UpdateSourceTrigger="PropertyChanged" 
         /> 
      </Label.Content> 
     </Label> 

標籤和中間XmlDataProvider

<Window.Resources> 
    <XmlDataProvider x:Key="CourseInfo" Source="Settings\CourseScema.xml"/> 
</Window.Resources> 

但得到這個錯誤,因爲我跟蹤使用System.Diagnostics.TextWriterTraceListener

System.Windows.Data Information: 41 : BindingExpression path error: '' property not found for 'current item of collection' because data item is null. This could happen because the data provider has not produced any data yet. BindingExpression:Path=/; DataItem=null; target element is 'Label' (Name=''); target property is 'Content' (type 'Object')

有什麼建議嗎?

回答

1

首先,你需要提供根XPathXmlDataProvider

<XmlDataProvider x:Key="CourseInfo" Source="Settings\CourseScema.xml" XPath="Course"/> 

其次,Title是一個屬性,所以你需要使用:

<Binding Source="{StaticResource CourseInfo}" 
         XPath="@Title" 
         UpdateSourceTrigger="PropertyChanged" 
         /> 

此外,確保您的xml文件的Build Action設置爲ContentCopy to Output Directory設置爲Copy alwaysCopy if newer