开发者

Issue with WPF DataBinding to XML file

开发者 https://www.devze.com 2022-12-14 10:40 出处:网络
I\'m in the process of creating a message application in WPF, as part of this I have a listbox which shows all the messages currently avaliable with the authors Title and Name. Anyway I\'m currently i

I'm in the process of creating a message application in WPF, as part of this I have a listbox which shows all the messages currently avaliable with the authors Title and Name. Anyway I'm currently in the developing but the data I wish to show isn't appearing but the titles do (Author: and Title:). Please be aware my XML file is a test that I know works from another project I have seen online.

Any help would be appreciated thanks.

XAML for the Databinding and ItemsSource template:

<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 Fil开发者_如何学编程e:

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


This should be changed to:

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

This should be changed to:

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

精彩评论

暂无评论...
验证码 换一张
取 消