Skip to main content

Microsoft Silverlight

Answered Question Can you parameterize an Image Source?RSS Feed

(0)

mathiasX_
mathiasX_

Member

Member

14 points

55 Posts

Can you parameterize an Image Source?

I am trying to dynamically set the source of an image depending on an integer set in a textbox...

private string license;

public int customerID = 1;

switch(customerID)

{case 1:

license="l1.jpg";

break;

case 2:

license="l2.jpg";

break;}

with

<Image Source="{Binding license}" ...

in the xaml but it didn't work. Any idea how I can set that up?

jay nanavati
jay nana...

Contributor

Contributor

3388 points

624 Posts

Answered Question

Re: Can you parameterize an Image Source?

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}"

Jay K Nanavaty
www.technologyopinion.com
Mark as answer if it helps. It will also help others...

mathiasX_
mathiasX_

Member

Member

14 points

55 Posts

Re: Can you parameterize an Image Source?

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.
 
 

mathiasX_
mathiasX_

Member

Member

14 points

55 Posts

Answered Question

Re: Can you parameterize an Image Source?

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>
 Thanks Jay!

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities