WP7 ImageBrush Binding problem: workaround
published on: 2/1/2011 | Views: N/A | Tags: Images Binding UI
by WindowsPhoneGeek
Lets say that you have a ListBox and want to set some opacity or another visual effect to its DataTemplate dynamically using ImageBrush. If you try to Bind the ImageSource property of the ImageBrush you will get an error. Then if you try to edit the ControlTemplate and TemplateBind the ImageSource you will again get an error.
Here is why:
The ImageBrush doesn't inherit from FrameworkElement so it can't be TemplateBound or Data Bound!
The good news is that you can get around this problem by rewriting your code a little and adding Converter.
Workaround
1. We will create the Converter. The code is as follows:
public class ImageBrushConverter : IValueConverter
{
#region IValueConverter Members
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
BitmapImage image = (BitmapImage)value;
ImageBrush imageBrush = new ImageBrush();
if (image != null)
{
imageBrush.ImageSource = image;
}
return imageBrush;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}2. Now we will create a sample DataSource for our ListBox. Like for example:
public class MyCollection
{
public ImageSource MyImage
{
get;
set;
}
public string Text
{
get;
set;
}
}
// Constructor
public MainPage()
{
InitializeComponent();
List<MyCollection> data = new List<MyCollection>()
{ new MyCollection(){Text = "First Item", MyImage = new BitmapImage(new Uri("/WP7SampleProject2;component/appbar.feature.email.rest.png", UriKind.Relative))},
new MyCollection(){Text = "Second Item", MyImage = new BitmapImage(new Uri("/WP7SampleProject2;component/appbar.feature.email.rest.png", UriKind.Relative))}
};
this.listBox.ItemsSource = data;
}
3. We will add some elements and Bindings to the DataTemplate of our ListBox:
<Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0">
<Grid.Resources>
<local:ImageBrushConverter x:Key="myConverter"/>
</Grid.Resources>
<ListBox x:Name="listBox">
<ListBox.ItemTemplate>
<DataTemplate>
<Border Background="{Binding MyImage,Converter={StaticResource myConverter}}" Height="100" Width="100">
<TextBlock Text="{Binding Text}"/>
</Border>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
That`s it. Now we have the desired ImageBrush set as a Background of the Border.
You can get the source here.
You can also follow us on Twitter @winphonegeek
Comments
Good job guys!
posted by: Kate on 2/1/2011 4:58:41 PM
Thank you so much for your help!
Perfect
posted by: Brandon on 7/26/2011 6:24:06 AM
This should be stickied EVERYWHERE.
thank u man
posted by: susant on 1/5/2012 6:05:07 PM
i had problem same as this ...but solved now...
i have one doubt
i started my project in wp7.1 environment the imagebrush Binding was working fine, but the time converted it to 7.0 environment, it started showing XamlParseError, i was felling what the hell...
it's good solution, but i want to know is that really a problem with versions or something else ?
one more thing that
<FrameworkElement.Resources>
<ResourceDictionary>
<bindings:ImageBrushConverter x:Key="myConverter" />
</ResourceDictionary>
</FrameworkElement.Resources>
Is that ok...
great job!
posted by: huntert on 2/13/2012 5:30:30 AM
thanks man, that did work!
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
