I'm quite new to WPF, and I'm trying to use a dataGrid
. Some of the data I'm trying to display is an image. First I tried to just bind the data like this:
DataGrid.ItemsSource = <some collection>
this resulted with a column containing the path to the Image, not displaying the image.
I started googling around, and I came up with the following:
I created a data template:
<DataTemplate x:Key="ImageDataTemplate">
<Image Source="{Binding Image}"/>
</DataTemplate>
I added to the AutoGeneratingColumn
event a function, and there I put the following code:
if (typeof(System.Windows.Media.ImageSource).IsAssignableFrom(e.PropertyType))
{
DataGridTemplateColumn dgtc = new DataGridTemplateColumn();
dgtc.Header = e.Column.Header;
dgtc.CellTemplate = this.FindResource("ImageDataTemplate") as DataTemplate;
e.Column = dgtc;
}
witch works fine, as long I call all Image properties in my classes Image
is there a way to be a bit more flexible? (I want a few images in a class, or having a meaning full name for the image property, and I don't like almost duplicated code)