Data Binding Images in Windows Phone
published on: 8/25/2011 | Views: N/A | Tags: Images Binding
by WindowsPhoneGeek
In this post I am going to demonstrate two ways in which you can data bind Images in Windows Phone, i.e. data bind a property of your business/data class to an Image Control. Displaying Images in a Windows Phone application is a common scenario, however most developers ask how to do this when having a data bound control.
The questions are pretty simple?
Question1: Lets say that we want to display images in a data bound ListBox/ ItemsControl/ListPicker/ ExpanderView (or whatever control that can be data bound) , so what is the best way of implementing this?
Question2: I want to bind a Image control to the property of my data class, so of what type my property should be?
Why binding to an Image control is confusing?
Binding to an Image control is confusing because:
- 1. The actual binding is done through the Source property of the Image control which is of type ImageSource but it in not usual to store object of type ImageSource.
- 2. It is not clear how to convert your pictures(.png, .jpg) into ImageSource.
- 3. It is not clear how the binding works and what should be the type of the property: <Image Source="{Binding MyProperty}"/>?
- 4. It is not clear whether or not you will need a Converter.
Answers
Option1: Property of type string
The easiest way is to use a property of type string that will represent the path to the actual picture in your application.
Example:
Data Source
NOTE: We will add two images with build action Content. Take a look at this post for more info: WP7 working with Images: Content vs Resource build action
public MainPage()
{
InitializeComponent();
List<ImageData> dataSource = new List<ImageData>()
{
new ImageData(){Name = "User1:", ImagePath="/Images/user1.png"},
new ImageData(){Name = "User2:", ImagePath="/Images/user2.png"}
};
this.list.ItemsSource = dataSource;
}
public class ImageData
{
public string ImagePath
{
get;
set;
}
public string Name
{
get;
set;
}
}
Data Binding
<ListBox x:Name="list">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<Image Source="{Binding ImagePath}" Stretch="None"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
Option2: Property of type Uri
Another option is to use a property of type Uri.
Example:
Data Source
public MainPage()
{
InitializeComponent();
List<ImageData1> dataSource1 = new List<ImageData1>()
{
new ImageData1(){Name = "User1:", ImagePath= new Uri("/Images/user1.png",UriKind.RelativeOrAbsolute)},
new ImageData1(){Name = "User2:", ImagePath= new Uri("/Images/user2.png",UriKind.RelativeOrAbsolute)}
};
this.list1.ItemsSource = dataSource1;
}
public class ImageData1
{
public Uri ImagePath
{
get;
set;
}
public string Name
{
get;
set;
}
}
}
Data Binding
<ListBox x:Name="list1">
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<TextBlock Text="{Binding Name}"/>
<Image Source="{Binding ImagePath}" Stretch="None"/>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
NOTE: You can simply use :<Image Source="{Binding ImagePath}" Stretch="None"/> . There is no need to add any additional BitmapImage or Converters!
Here is the full source code:
I hope that the post was helpful.
You can also follow us on Twitter @winphonegeek
Comments
Thanks for the info
posted by: Rahmed Kumar on 8/27/2011 12:51:12 PM
Thanks for the info.
What about BitmapCache
posted by: Daniel on 3/16/2012 10:32:21 AM
Thanks for the info. I also want to know when I should use BitmapCache. I display a lot of images in my app and sometimes my images flare. I bind it like you describe in option 2. What could I do?
Image Control in Windows Phone 7 Development
posted by: Vein Douglas on 5/11/2012 5:50:43 PM
Thank you Very much for this article it helped me lot in understanding image control in windows phone 7 development, this has become my Best material in Image control in Windows Phone Development topic. You made this very simple and understandable. There are some goods articles too helped me lot in completing my task here I'm sharing that posts link... http://mobile.dzone.com/articles/image-control-silverlight
Thanks again for your wonderful post!
Our Top Articles & Free books
- Our FREE e-book: "Windows Phone Toolkit In Depth" 2nd edition
- 400+ Windows Phone Development articles in our Article Index
- 21 WP7 Toolkit in Depth articles covering all controls
- 12 WP7 Coding4Fun Toolkit in Depth articles covering all controls
- Performance Tips when creating WP7 apps
- Creating a WP7 Custom Control in 7 Steps
- WP7 working with VisualStates: How to make a ToggleSwitch from CheckBox
- What makes a WP7 App successful
- Creating theme friendly UI in WP7 using OpacityMask
- Implementing Windows Phone 7 DataTemplateSelector and CustomDataTemplateSelector
- All about Splash Screens in WP7 – Creating animated Splash Screen
- Getting Started with Unit Testing in Silverlight for WP7
- WP7 WatermarkedTextBox custom control
Our Top Tips & Samples
- All about WP7 Isolated Storage series
- WP7 Dynamically Generating DataTemplate in code
- 5 tips for a successful WP7 Marketplace submission
- WP7: Navigating to a page in different assembly
- WP7 ContextMenu: answers to popular questions
- WP7 ListBox: answers to popular questions
- WP7 working with Images: Content vs Resource build action
- WP7 Element Binding samples
- WP7 working with XML: reading, filtering and databinding
- Drawing in WP7: #2 Drawing shapes with finger
- WP7 TextBox Light theme problems - the solution
- Changing the WP7 Panorama Background Image dynamically with Animation
