Skip to main content
Home Forums Silverlight Programming Silverlight Controls and Silverlight Toolkit Can you parameterize an Image Source?
3 replies. Latest Post by mathiasX_ on July 2, 2009.
(0)
mathiasX_
Member
14 points
55 Posts
07-02-2009 2:49 PM |
I am trying to dynamically set the source of an image depending on an integer set in a textbox...
private
public
{
license=
<
in the xaml but it didn't work. Any idea how I can set that up?
jay nana...
Contributor
3388 points
624 Posts
07-02-2009 2:57 PM |
Class ImageLicense
string license {get; set;}
}
ImageLicense myLic=new ImageLicense();
In your switch case ,
myLic.license="l1.jpg";
This.DataContext=myLic
<Image Source="{Binding license}"
07-02-2009 3:40 PM |
Didn't quite work. Here is what I have...
xaml
<sd:DockedSplitContainer Dock="Left" Margin="0,0,-1000,0" Width="Auto"> <sd:SplitContainer Width="Auto"> <sd:WindowGroup Background="#FF37466D" > <sd:ToolWindow Title="Identification" x:Name="Identification" Background="#FF37466D" FloatingSize="380,220"> <Image Name="testimage" Source="{Binding license}" OpacityMask="#FF37466D" /> </sd:ToolWindow> </sd:WindowGroup> <sd:WindowGroup Background="#FF000000" > <sd:ToolWindow Title="Contact Info" x:Name="ContactInfo" Background="#FF37466D" FloatingSize="400,280"> <Canvas Name="testCanvas" HorizontalAlignment="Stretch" VerticalAlignment="Top">...etc
in the c# code behind I have...
class ImageLicense { public string license {get; set;} } and in the pageload area I have... ImageLicense myLic = new ImageLicense(); switch(customerID) { case 1: myLic.license = "l1.jpg"; testimage.DataContext = myLic; break; case 2: myLic.license = "l2.jpg"; testimage.DataContext = myLic; break; case 3: myLic.license = "l3.jpg"; testimage.DataContext = myLic; break; case 4: myLic.license = "l4.jpg"; testimage.DataContext = myLic; break; case 5: myLic.license = "l5.jpg"; testimage.DataContext = myLic; break; } //this.DataContext = myLic; testimage.DataContext = myLic; Didn't seem to be able to link somehow in either the this or testimage reference for setting the datacontext so still no images...and yes the images have been added to the project and show up fine if I explicitly set the source to the file name.
class ImageLicense { public string license {get; set;} }
and in the pageload area I have...
ImageLicense myLic = new ImageLicense(); switch(customerID) { case 1: myLic.license = "l1.jpg"; testimage.DataContext = myLic; break; case 2: myLic.license = "l2.jpg"; testimage.DataContext = myLic; break; case 3: myLic.license = "l3.jpg"; testimage.DataContext = myLic; break; case 4: myLic.license = "l4.jpg"; testimage.DataContext = myLic; break; case 5: myLic.license = "l5.jpg"; testimage.DataContext = myLic; break; } //this.DataContext = myLic; testimage.DataContext = myLic;
Didn't seem to be able to link somehow in either the this or testimage reference for setting the datacontext so still no images...
and yes the images have been added to the project and show up fine if I explicitly set the source to the file name.
07-02-2009 6:36 PM |
I was close the first time around and this finally worked...
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; namespace SilverlightApplication4 { public partial class MainPage : UserControl { private int customerID = 5; public MainPage() { InitializeComponent(); Loaded += new RoutedEventHandler(MainPage_Loaded); } void MainPage_Loaded(object sender, RoutedEventArgs e) { LicenseImage myLicense = new LicenseImage(); switch (customerID) { case 1: myLicense.License = "l1.jpg"; break; case 2: myLicense.License = "l2.jpg"; break; case 3: myLicense.License = "l3.jpg"; break; case 4: myLicense.License = "l4.jpg"; break; case 5: myLicense.License = "l5.jpg"; break; } testimage.DataContext = myLicense; } } }
with this class file...
using System; public class LicenseImage { private string license; public string License { get { return license; } set { license = value; } } }
and this xaml...
<UserControl x:Class="SilverlightApplication4.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:sd="clr-namespace:Divelements.SandDock;assembly=Divelements.SandDock" Width="400" Height="300"> <Grid x:Name="LayoutRoot" Background="White"> <sd:DockSite x:Name="dockSite" Margin="0,36,0,0" Width="Auto"> <sd:DockedSplitContainer Dock="Top" ContentSize="220" Width="Auto"> <sd:SplitContainer Width="Auto"> <sd:WindowGroup> <sd:ToolWindow Title="TestTool"> <Image Name="testimage" Source="{Binding License}" OpacityMask="#FF37466D" /> </sd:ToolWindow> </sd:WindowGroup> </sd:SplitContainer> </sd:DockedSplitContainer> </sd:DockSite> <Canvas Height="Auto" Margin="1,2,8,0" VerticalAlignment="Top" Background="#FF2F3F59"> <TextBlock Height="17" Width="61" Canvas.Left="8" Canvas.Top="8" Text="Search" TextWrapping="Wrap" FontSize="14" Foreground="#FFDCD3D3"/> </Canvas> </Grid> </UserControl>