Windows Phone Zone
Windows Phone Zone is brought to you in partnership with:
spacer spacer Sumit Dutta
  • Bio
  • @sumitdutta21

I am mastered in computer application (MCA)and working as Architect in one of the leading software services company in India. I have more than 7 years of experience in software development. Sumit is a DZone MVB and is not an employee of DZone and has posted 22 posts at DZone. You can read more from them at their website. View Full User Profile

Windows Phone: Binding Contact Image to ListBox

07.18.2012
spacer Email
Views: 1769
  • Tweet
  • spacer
It's so easy to get up and running with your first Windows Phone app!  Just pick up the FREE toolkit from Microsoft, register at AppHub, and start checking out some fundamental tutorials.

Related MicroZone Resources

Windows Phone SDK 7.1 FREE Download

Visual Studio Express 2012 for Win 8 is FREE!

Insider Tips for Windows 8 Development

Early Access! Windows 8 SDK

Like this piece? Share it with your friends:

| More

In previous article Part 67 - Windows Phone - Get Contact Details I discussed how to get contact details. In this article I will explain how to get image from Contacts and bind it to ListBox.

Let's write code:

Step 1:  Add a ListBox and a Image below Listbox inside contentpanel of MainPage.xaml. ListBox will contain a image and textblock. Image inside listbox will hold the image of Contacts and textblock inside listbox will hold Contacts displayname. The image element outside ListBox will be used to show how to bind the Contacts image to individual image element.

<ListBox Grid.Row="1" Name="ContactResultsData" ItemsSource="{Binding}" "400" Margin="10,224,0,0" VerticalAlignment="Top">
   <ListBox.ItemTemplate>
      <DataTemplate>
         <StackPanel Orientation="Horizontal" >
            <Border BorderThickness="2" HorizontalAlignment="Left" VerticalAlignment="Center" BorderBrush="{StaticResource PhoneAccentBrush}" >
               <Image Source="{Binding Converter={StaticResource ContactPictureConverter}}" "48" "48" Stretch="Fill" />
            </Border>
            <TextBlock Name="ContactResults" Text="{Binding Path=DisplayName, Mode=OneWay}" FontSize="{StaticResource PhoneFontSizeExtraLarge}" Margin="18,8,0,0" />
         </StackPanel>
      </DataTemplate>
   </ListBox.ItemTemplate>
</ListBox>


<Border BorderThickness="2" "60" Margin="10,-400,0,0" HorizontalAlignment="Left" BorderBrush="{StaticResource PhoneAccentBrush}" >
   <Image Name="Picture" "60" "60" HorizontalAlignment="Left" />
</Border>

Step 2: Add namespace of application in phone:PhoneApplicationPage element in MainPage.xaml.

xmlns:MyApp="clr-namespace:WP_Display_Contacts_Photo"

Step 3: Add below using directive in MainPage.xaml.cs

 

using Microsoft.Phone.UserData;
using System.Windows.Media.Imaging;

Step 4: Add below code inside constructor of MainPage.xaml.cs.


 

ContactResultsData.DataContext = null;
Contacts cons = new Contacts();
cons.SearchCompleted += new EventHandler<ContactsSearchEventArgs>(Contacts_SearchCompleted);
cons.SearchAsync("", FilterKind.None, "Contacts Image Test");

Step 5: Contacts_SearchCompleted method has two sections first section is to bind the first contact image to image element in MainPage.xaml and the second section is to bind the contact result to Listbox.

void Contacts_SearchCompleted(object sender, ContactsSearchEventArgs e)
{
   try
   {
      //Bind image to image element
      Contact con = e.Results.First();
      BitmapImage img = new BitmapImage();
      img.SetSource(con.GetPicture());
      Picture.Source = img;

      //Bind the results to the list box that displays them in the UI
      ContactResultsData.DataContext = e.Results;
   }
   catch (System.Exception)
   {
   }
}

Step 6: Create below class below MainPage class of MainPage.xaml.cs. ContactPictureConverter class bind the data.

public class ContactPictureConverter : System.Windows.Data.IValueConverter
{
   public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
      Contact c = value as Contact;
      if (c == null) return null;
      System.IO.Stream imageStream = c.GetPicture();
      if (null != imageStream)
      {
         return Microsoft.Phone.PictureDecoder.DecodeJpeg(imageStream);
      }
      return null;
   }

   public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
   {
      throw new NotImplementedException();
   }
}

Step 7: Now run the appliction and you will notice the Contact image is binded to ListBox.

This ends the article of binding Contacts image to Listbox.
Published at DZone with permission of Sumit Dutta, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)

Tags:
  • Windows Phone
The Windows Phone Microzone, which is supported by Microsoft, is your one-stop-shop for news, tutorials, perspectives, and research on the mobile platform that is making waves in smartphone ecosystem.
gipoco.com is neither affiliated with the authors of this page nor responsible for its contents. This is a safe-cache copy of the original web site.