I created an ImageColumn for a DataGrid object. However I have no cluehow to bind a relative image path to it. I need something like this, but in C#.
<Image Source="pack://application:,,,/Images/Folder-icon.png"/>
This is how I create my ImageColumn
public static DataGridTemplateColumn createImageColumn(string header, Binding b, Size s)
{
DataTemplate dt = new DataTemplate();
dt.DataType = typeof(Image);
FrameworkElementFactory dtFactory = new FrameworkElementFactory(typeof(StackPanel));
dtFactory.Name = "stack";
dtFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);
FrameworkElementFactory imageHolder = new FrameworkElementFactory(typeof(Image));
imageHolder.Name = header;
imageHolder.SetValue(Image.SourceProperty, b);
imageHolder.SetValue(Image.WidthProperty, s.Width);
imageHolder.SetValue(Image.HeightProperty, s.Height);
imageHolder.SetValue(Image.HorizontalAlignmentProperty, HorizontalAlignment.Center);
imageHolder.SetValue(Image.VerticalAlignmentProperty, VerticalAlignment.Center);
dtFactory.AppendChild(imageHolder);
dt.VisualTree = dtFactory;
DataGridTemplateColumn imageColumn = new DataGridTemplateColumn();
imageColumn.Header = header;
imageColumn.CellTemplate = dt;
return imageColumn;
}
And this is my current binding.
Binding b = new Binding();
b.Path = new PropertyPath("IMAGE_PATH");
b.RelativeSource = new RelativeSource(RelativeSourceMode.Self);
Edit: "IMAGE_PATH" is another column of the DataGrid. It holds the relative path to the image file (relative from the applications root). If I add an absolute path instead, it works. But not with a relative path.
More about the project: The datagrid holds item information (Part number, image, description, price). The user should be able to add images. The images will be copied into the application like:
APPLICATION_ROOT/Images/Item/file.png
This path will be saved in the database at the "IMAGE_PATH" column.
Images/Item/file.png
Later this path will be used to draw the image in the ImageColumn. However it's a relative path and it's only working with an absolute path yet.
Hope this makes it more clear. Thank you!