I have a list of objects.
Each object has a property named “ext” that represents the file type. (PDF, CSV, etc.)
Programmatically before binding the itemsource of the datagrid to the collection of objects, I’m dynamically creating the datagrid columns using datagridtemplatecolumn objects.
I have a DataTemplate resource that is basically an image:
<DataTemplate x:Key="imageThumb">
<Image x:Name="docImage" Width="25" Height="25" Source="/MyApp;component/images/pdf-icon.png">
<Image.Effect>
<DropShadowEffect ShadowDepth="1" BlurRadius="1" Opacity="0.5"/>
</Image.Effect>
</Image>
</DataTemplate>
Then in my code behind while dynamically creating the datagrid template columns:
DataTemplate imageTemplate = (DataTemplate)this.Resources["imageThumb"];
DataGridTemplateColumn docType = new DataGridTemplateColumn();
docType.Header = "Doc Type";
docType.CellTemplate = imageTemplate;
targetDataGrid.Columns.Add(docType);
When I simply set this as the cell template of the datagridtemplate column all is well but obviously every single row has a PDF icon in the column.
I’m wanting to dynamically change the source path of this Image” based on the extension property of the object that is bound to the datagrid row.
Is there any way to do this?
Grab the code for the
StringToObjectConverterfrom this blog Yet another blog about IValueConverterIn your Xaml resources set up your set of known icon images in a
StringToObjectConverterlike this:(You could of course fill this list of BitmapImages programmatically if you prefer).
Now you can use this converter with binding in you
Now the image will track with changes to
docTypedynamically.