Articles in this section
Category / Section

How to use image path in WinRT DataGrid as an image source in GridImageColumn?

2 mins read

GridImageColumn helps you bind the types that are supported by Image.Source,
such as BitMapImage. When you bind a format other than the supported format, such as name of the image or file path of the image, GridImageColumn does not load the images. In such cases, you can use converter in ValueBinding for GridImageColumn that converts the string path to support Image source format. The following code example illustrates how to bind the image name or file path to GridImageColumn with the help of ValueBinding along with Converter to convert image name to BitMapImage.

XAML

<syncfusion:SfDataGrid 
            AutoGenerateColumns="False" 
            ItemsSource="{Binding OrderInfoCollection}" 
            ColumnSizer="Auto">
        <syncfusion:SfDataGrid.Columns>
        <syncfusion:GridTextColumn 
                MappingName="OrderID" HeaderText="Order ID"/>
        <syncfusion:GridTextColumn 
            MappingName="CustomerID" HeaderText="Customer ID"/>
        <syncfusion:GridTextColumn 
            MappingName="CustomerName" HeaderText="Name of Customer"/>
        <syncfusion:GridTextColumn 
            MappingName="ShipCountry" HeaderText="Ship Country"/>
        <syncfusion:GridTextColumn 
                MappingName="ShipCity" HeaderText="Ship City"/>
        <syncfusion:GridImageColumn 
                MappingName="ImageLink" ImageHeight="50"
                ImageWidth="50"                            
                ValueBinding="{Binding Path=ImageLink, 
                Converter={StaticResource stringToImageConverter}}" />
        </syncfusion:SfDataGrid.Columns>
</syncfusion:SfDataGrid>

SfDataGrid bounded to collection of OrderInfo and OrderInfo class have a property ImageLink that contains the image names on the Project.

Gridimage column

Following is the converter that converts image name (placed in the Project) into BitMapImage.

C#

class StringToImageConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            if (value != null)
            {
                string imagename = value as string;
                return new BitmapImage(new Uri(string.Format(@"..\..\Image\{0}", imagename), UriKind.Relative));
            }
            return null;
        }
        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

In WinRT and UWP you can give file path directly in StringToImageConveter. Convert method code example for WinRT and UWP platform is as follows.

public object Convert(object value, Type targetType, object parameter, string language)

        {

            if (value != null)

            {

                string imagename = value as string;

                 return new Uri(string.Format(@"..\..\Image\{0}" ,

                  imagename), UriKind.Relative));           

                       }

            return null;

        }

The following is the screenshot for image column that loads images in GridImage Column.

                                                                                                                image column

 

Sample Links:

WPF

WRT

UWP

 

 

Did you find this information helpful?
Yes
No
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment
Access denied
Access denied