This is a migrated thread and some comments may be shown as answers.

Using a converter for string to image

4 Answers 343 Views
GridView
This is a migrated thread and some comments may be shown as answers.
Peter
Top achievements
Rank 1
Peter asked on 05 Apr 2012, 01:21 AM
Hi,

I have a RadGridView that gets bound to a DataTable (aside: this may not be the proper way to bind, but the UserControl is passed _wg) with this line:
rgvGrid.ItemsSource = _wg.GridData.Tables[0];
One of the columns has a string that I'd like to show an image dependant on what the string is. A job for a converter I thought...
My Converter class is (I've taken out the case statements to ensure an image is returned):
public class RequestColumnConverter : IValueConverter
{
    public object Convert(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        if (value != null)
        {
            string name = (string)value;
 
            var requestImage = new Image()
            {
                Height = 16,
                Width = 16,
                HorizontalAlignment = System.Windows.HorizontalAlignment.Center,
            };
 
            requestImage.Source = new BitmapImage(new Uri("pack://application:,,,/Images/service.png"));
            return requestImage;
        }
        else return null;
    }
 
    public object ConvertBack(object value, Type targetType,
        object parameter, CultureInfo culture)
    {
        return null;
    }
}

My xaml file contains:
<telerik:GridViewDataColumn Header="Type" IsReadOnly="True" UniqueName="SCSRequestTypeName" DataMemberBinding="{Binding Path=SCSRequestTypeName, Converter={StaticResource requestColumnConverter}}">
     <telerik:GridViewDataColumn.CellTemplate>
             <DataTemplate>
                <Image Source="{Binding SCSRequestTypeName, Converter={StaticResource requestColumnConverter}}"/>
            </DataTemplate>
     </telerik:GridViewDataColumn.CellTemplate>
</telerik:GridViewDataColumn>
This doesn't work. When I add the converter to DataMemberBinding (as shown above), the program enters the converter, but it doesn't enter the converter if I simply have it listed in the Image Source line (also shown).

Have you any suggestions how to get this to work?

Thanks

4 Answers, 1 is accepted

Sort by
0
Vlad
Telerik team
answered on 05 Apr 2012, 08:29 AM
Hi,

 This sounds strange indeed! Do you get the same behavior when using this image (and the converter for the binding) in a plain ListBox as well?

Greetings,
Vlad
the Telerik team
Sharpen your .NET Ninja skills! Attend Q1 webinar week and get a chance to win a license! Book your seat now >>
0
Peter
Top achievements
Rank 1
answered on 10 Apr 2012, 05:07 AM
Well, using the same code as above, I made a change to the way it binds so the xaml creates a class for the binding:
<UserControl.Resources>
        <local:DataViewRepository x:Key="MainRepo" />
        <local:TypeColumnConverter x:Key="requestColumnConverter" />
    </UserControl.Resources>
    <Grid>
        <telerik:RadGridView ItemsSource="{Binding Source={StaticResource MainRepo}, Path=ADataViewVar}">
...
And it fixed itself. Obviously something was playing up in the binding but it still seemed like it should've worked.
Thanks
0
Peter
Top achievements
Rank 1
answered on 10 Apr 2012, 05:13 AM
Come to think of it, it's binding to a DataView now rather than a DataTable. And sure enough, if I change back to a DataTable the images disappear. Why would that be the cause? I don't think I'll need to use DataView's filter and sort capabilities so from my limited knowledge the DataView should be pretty similar to the DataTable?
0
Accepted
Vlad
Telerik team
answered on 10 Apr 2012, 06:37 AM
Hello,

 The DataView will expose properties since it is ICustomTypeDescriptor while with DataTable you have indexers - your binding will be invalid (you need square brackets).

Regards,
Vlad
the Telerik team

Explore the entire Telerik portfolio by downloading the Ultimate Collection trial package. Get it now >>

Tags
GridView
Asked by
Peter
Top achievements
Rank 1
Answers by
Vlad
Telerik team
Peter
Top achievements
Rank 1
Share this question
or