在Windows Phone应用程序中将图像从xml绑定到列表框

时间:2013-01-03 11:21:05

标签: windows-phone-7 linq-to-xml

我的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;

你可以帮我看看如何绑定图像

1 个答案:

答案 0 :(得分:1)

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

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