Vivek Maskara
Vivek Maskara

Reputation: 1092

binding images from xml to listbox in windows phone application

My xml file look's like this

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

My XAML file looks like

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

in my xaml.cs file i have add

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;

can u help me how to bind a image

Upvotes: 0

Views: 548

Answers (1)

nkchandra
nkchandra

Reputation: 5557

Just change the name ImageSource to Image (Image is the property name of your Person class .. 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>

Upvotes: 1

Related Questions