WPF DataBinding到XML文件的问题

时间:2009-12-14 10:22:18

标签: c# wpf xml data-binding

我正在WPF中创建一个消息应用程序,作为其中的一部分,我有一个列表框,显示当前可用作者标题和名称的所有消息。无论如何,我目前正处于开发阶段,但我希望展示的数据不会出现,但标题会出现(作者:和标题:)。请注意我的XML文件是一个测试,我知道这是我在网上看过的另一个项目。

感谢任何帮助。

Databinding和ItemsSource模板的XAML:

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

1 个答案:

答案 0 :(得分: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>