Image in silverlight datagrid

Posted on 2008-11-17
Last Modified: 2013-12-17
I'm a Siliverlight newbie.

I want to create a datagrid with a different photo for each row.  These photos will reside in the c:\Photos\xxx\ directory on the web server, where xxx represents a unique ID for each data row.

This xaml works, where I've just used a test file for the photo:

                            <Image x:Name ="TestImage" Source ="100_0117.JPG" Height ="50"/>

But what is the syntax to make that image path vary for each row?

Question by:ReeseWatt
    1 Comment

    Accepted Solution

    I solved this myself. It required a converter. Add a converter as a static resource and then use that converter to bring in the data. In this case, I named my photo files ItemID.jpg, so that I could bring in the appropriate by just knowing the ItemID of the current row.  Here's the applicable XAML and C# code

    <HMSRemnants_svcSayHello:RemnantsTest x:Key="RemnantsTestDS" d:IsDataSource="True"/>
    <local:SlabID2ImageConverter x:Key="SlabID2ImageConverter"/>

    <StackPanel x:Name="OurStack" Orientation="Vertical">
    <TextBox x:Name="InputText"/>
    <TextBlock x:Name="OutputText"/>
    <Button x:Name="CallServiceButton" Content="Call WCF" Click="CallServiceButton_Click"/>
    <data:DataGrid x:Name="theDataGrid" AlternatingRowBackground="Beige" Grid.Row="2" Grid.Column="1" Height=" 600" Width="800" CanUserResizeColumns="True" AutoGenerateColumns="False">
    <data:DataGridTextColumn Header="InventTypeID" Width="100" Binding="{Binding InventTypeID}" />
    <data:DataGridTemplateColumn Header="ImageColumn" DisplayIndex="1" >
    <Image Source="{Binding ItemID, Converter={StaticResource SlabID2ImageConverter}}" Height="50" />
    <?xml:namespace prefix = local />

    public class SlabID2ImageConverter : IValueConverter
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    Uri uri = new Uri("/Photos/" + value.ToString()+ ".jpg", UriKind.Relative);
    return new BitmapImage(uri);
    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    throw new NotImplementedException();


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    Training Course: Java/J2EE and SOA

    This course will cover both core and advanced Java concepts like Database connectivity, Threads, Exception Handling, Collections, JSP, Servlets, XMLHandling, and more. You'll also learn various Java frameworks like Hibernate and Spring.

    For most people, the WrapPanel seems like a magic when they switch from WinForms to WPF. Most of us will think that the code that is used to write a control like that would be difficult. However, most of the work is done by the WPF engine, and the W…
    Entering time in Microsoft Access can be difficult. An input mask often bothers users more than helping them and won't catch all typing errors. This article shows how to create a textbox for 24-hour time input with full validation politely catching …
    This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA.…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    737 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now