2009-12-14 49 views
0

我正在創建WPF中的消息應用程序的過程中,作爲其中的一部分,我有一個列表框,顯示當前可用的作者標題和名稱的所有消息。無論如何,我目前正在發展,但我希望展示的數據並沒有出現,但標題做(作者:和標題:)。請注意,我的XML文件是一個測試,我知道來自另一個項目,我在網上看過。問題與WPF DataBinding到XML文件

任何幫助,將不勝感激謝謝。

XAML的數據綁定和的ItemsSource模板:

<XmlDataProvider x:Key="Announcement" Source="Data/People.xml" XPath="People"/> 
     <DataTemplate x:Key="AnnouncementTemplate"> 
      <StackPanel Orientation="Horizontal"> 
       <TextBlock Text="Author: " FontWeight="Bold"/> 
       <TextBlock> 
        <TextBlock.Text> 
         <Binding XPath="./ImageFile"/> 
        </TextBlock.Text> 
       </TextBlock> 
       <TextBlock Text="Title: " FontWeight="Bold"/> 
       <TextBlock Text="{Binding XPath=./Notes/}"/> 
      </StackPanel> 
     </DataTemplate> 
<ListBox Style="{StaticResource SpecialListStyle}" 
        Name="listBox1" 
        Grid.Row="1" 
        Margin="10,10,10,10" 
        IsSynchronizedWithCurrentItem="True" 
        SelectedIndex="0" 
        ItemContainerStyle="{StaticResource SpecialListItem}" 
        Foreground="Black" 
        ItemsSource="{Binding Source={StaticResource Announcement}, XPath=Person}" 
        ItemTemplate="{StaticResource AnnouncementTemplate}"/> 

XML文件:

<?xml version="1.0" encoding="utf-8" ?> 
<People> 
    <Person Name="Capt. Monterey Jack"> 
    <ImageFile>Data/MontereyJack.jpg</ImageFile> 
    <Notes>The Captain loves his cheese, but hates milk.</Notes> 
    </Person> 
    <Person Name="Dr. Disco Fortuna"> 
    <ImageFile>Data/DiscoFortuna.jpg</ImageFile> 
    <Notes>He disco dances when he's not selling organic vacuum filters.</Notes> 
    </Person> 
    <Person Name="Professor Huunkel Froobenhammer"> 
    <ImageFile>Data/HuunkelFroobenhammer.jpg</ImageFile> 
    <Notes>Huunkel designed a better mousetrap, but lost the blueprint.</Notes> 
    </Person> 
</People> 

回答

2

這應改爲:

<XmlDataProvider x:Key="Announcement" Source="Data/People.xml" XPath="People/Person"/> 

這應改爲:

<StackPanel Orientation="Horizontal">     
<TextBlock Text="Author: " FontWeight="Bold"/> 
<TextBlock Text="{Binding XPath=ImageFile}" > 
<TextBlock Text="Title: " FontWeight="Bold"/> 
<TextBlock Text="{Binding XPath=Notes}"/> 
</StackPanel>