0

我的 xml 文件看起来像这样

<people><person> <firstname>venakt</firstname> <lastname>es</lastname> <age>27</age> <Image>http://www.livetut.com/wp-content/uploads/2012/06/Logo1.png</Image> </person></people>

我的 XAML 文件看起来像

<ListBox.ItemTemplate>
 <DataTemplate>
  <StackPanel>
    <Image Source="{Binding ImageSource}" Height="120" Width="120" HorizontalAlignment="Center" VerticalAlignment="Center" Tap="textBlock1_tap" />
    <TextBlock Text="{Binding UserName}" Style="{StaticResource PhoneTextSubtleStyle}" Width="100" TextAlignment="Center"/>
  </StackPanel>
 </DataTemplate>
</ListBox.ItemTemplate>

在我的 xaml.cs 文件中,我添加了

public string Image { get { return Image; } set { Image = value; } }



XDocument loadedData = XDocument.Load("People.xml");
    var data = from query in loadedData.Descendants("person")
                  select new Person
                  {
                      FirstName = (string)query.Element("firstname"),
                      LastName = (string)query.Element("lastname"),
                      Image= query.Element("Image").Attribute("url").Value

                  };
    listBox.ItemsSource = data;

你能帮我如何绑定图像吗

4

1 回答 1

1

只需将名称 ImageSource 更改为 Image (Image 是您的 Person 类的属性名称.. 所以)

<ListBox.ItemTemplate>
 <DataTemplate>
  <StackPanel>
    <Image Source="{Binding Image}" Height="120" Width="120" HorizontalAlignment="Center" VerticalAlignment="Center" Tap="textBlock1_tap" />
    <TextBlock Text="{Binding UserName}" Style="{StaticResource PhoneTextSubtleStyle}" Width="100" TextAlignment="Center"/>
  </StackPanel>
 </DataTemplate>
</ListBox.ItemTemplate>
于 2013-01-03T12:15:26.320 回答