Displaying an Image in a Field

In your xamDataGrid™, you may want a Field to display an image. To do this, you need to override the CellValuePresenter's template.

The following procedure assumes you have the basic knowledge on how to add a xamDataGrid to a page. For more information, see the Adding xamDataGrid to Your Page topic.

To display an image in a field:

  1. Create a file in your application named Products.xml with the following content, which has a "Logo" attribute pointing to the images that we want to load.


    To reflect where the images are located on your machine, you must modify the content of the file to change the path for the logo images.

  2. Create an XmlDataProvider in your page’s resources section which will be used as a data source.

    In XAML:

    <XmlDataProvider x:Key="ProductsData" Source="/WpfApplication1;component/Data/Products.xml" XPath="/Products" />

    You may need to change the resource path and the XPath value in the definition above depending on your application specific paths.

  3. Create a Style that targets the CellValuePresenter class in your page’s resources section. You will also need to give the style a Key of "ProductImage". The style should look similar to the following example code.

    In XAML:

    <Style TargetType="{x:Type igDP:CellValuePresenter}" x:Key="ProductImage">
        <Setter Property="Template">
                <ControlTemplate TargetType="{x:Type igDP:CellValuePresenter}">
                    <Grid Width="{TemplateBinding Width}" Height="{TemplateBinding Height}">
                        <Image Margin="{TemplateBinding Padding}"
                            HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                            VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                            Source="{Binding RelativeSource={RelativeSource TemplatedParent}, Path=Content}" />
  4. Use the following XAML code to create an instance of XamDataGrid and set its data source.

    In XAML:

    <igDP:XamDataGrid DataSource="{Binding Source={StaticResource ProductsData}, XPath=Product}">
  5. Inside the XamDataGrid tags, place the following XAML code. This lays out the fields and also sets the "Logo" FieldSettings to the CellValuePresenterStyle to the Style created above.

    In XAML:

        <igDP:FieldLayoutSettings AutoGenerateFields="False" />
                <igDP:Field Name="ProductName" Label="Product Name">
                        <igDP:FieldSettings CellMinWidth="200" CellWidth="200" />
                <igDP:Field Name="Logo" Label="Product Logo">
                        <igDP:FieldSettings CellMinWidth="200" CellWidth="200"
                                            CellValuePresenterStyle="{StaticResource ProductImage}" />
  6. Build and run the project and you should see something similar to the following image.

    displaying an image in xamdatapresenter field