How to write the dynamic source for image control in style for a button (APP.XAML) in silverlight 3?
views:
310answers:
1
Q:
How to write the dynamic source for image control in style for a button (APP.XAML) in silverlight 3?
A:
I work with WPF, not Silverlight (yet!), but it seems like this is 'core' enough it should be applicable in both WPF/Silverlight scenarios. I could be wrong though! :)
Have you looked at using a DataTrigger? This will allow you to style the source of the image based on a data value in your DataContext.
This sample assumes we've got a DataContext set with a Property called "Frame". The Image.Source is set based on the value Frame.
<Window x:Class="WpfApplication1.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<StackPanel>
<Image>
<Image.Style>
<Style>
<!-- style the image source based on some property in our data context, though
replace static paths with pack URL-->
<Style.Triggers>
<DataTrigger Binding="{Binding Frame}" Value="1">
<Setter Property="Image.Source" Value="C:\dev\images\sample\image1.jpg"/>
</DataTrigger>
<DataTrigger Binding="{Binding Frame}" Value="2">
<Setter Property="Image.Source" Value="C:\dev\images\sample\image2.jpg"/>
</DataTrigger>
</Style.Triggers>
</Style>
</Image.Style>
</Image>
</StackPanel>
</Window>
Instead of referring to the images using static file paths, you will want to replace with a pack:// uri. See Refer to Resources in WPF With a Pack URI for more information.
Zach Bonham
2009-09-29 12:46:44