繁体   English   中英

WPF DataBinding到XML文件的问题

[英]Issue with WPF DataBinding to XML file

我正在WPF中创建消息应用程序,在此过程中,我有一个列表框,其中显示了所有当前可用的带有作者Title和Name的消息。 无论如何,我目前正在开发中,但是我希望显示的数据没有出现,但标题却出现了(作者:和标题:)。 请注意,我的XML文件是我在网上看到的另一个项目中可以正常工作的测试。

任何帮助,将不胜感激谢谢。

用于数据绑定和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>

应将其更改为:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM