Adding datagrid cell images at runtime doesn't appear in cell
-
I'm trying to add images to a column in a WPF datagrid which are read from a folder and not added as a static resource. The datagrid shows the path to the image in debug but the cell is empty. XAML:
Code that describes the objects for the datagrid:
public class Scanners : INotifyPropertyChanged
{
#region dongleNum related infoprivate UInt32 thisIPAddress; public UInt32 IPAddress { get { return thisIPAddress; } set { thisIPAddress = value; NotifyPropertyChanged("IPAddress"); } } private UInt64 thisMACAddress; public UInt64 MACAddress { get { return thisMACAddress; } set { thisMACAddress = value; NotifyPropertyChanged("MACAddress"); } }
#endregion
#region Device related info
private string thisPortName; public string PortName { get { return thisPortName; } set { thisPortName = value; NotifyPropertyChanged("PortName"); } }
#endregion
#region Driver related info
private string thisDeviceGuid; public string DeviceGuid { get { return thisDeviceGuid; } set { thisDeviceGuid = value; NotifyPropertyChanged("DeviceGuid"); } }
#endregion
#region Presentation related
-
I'm trying to add images to a column in a WPF datagrid which are read from a folder and not added as a static resource. The datagrid shows the path to the image in debug but the cell is empty. XAML:
Code that describes the objects for the datagrid:
public class Scanners : INotifyPropertyChanged
{
#region dongleNum related infoprivate UInt32 thisIPAddress; public UInt32 IPAddress { get { return thisIPAddress; } set { thisIPAddress = value; NotifyPropertyChanged("IPAddress"); } } private UInt64 thisMACAddress; public UInt64 MACAddress { get { return thisMACAddress; } set { thisMACAddress = value; NotifyPropertyChanged("MACAddress"); } }
#endregion
#region Device related info
private string thisPortName; public string PortName { get { return thisPortName; } set { thisPortName = value; NotifyPropertyChanged("PortName"); } }
#endregion
#region Driver related info
private string thisDeviceGuid; public string DeviceGuid { get { return thisDeviceGuid; } set { thisDeviceGuid = value; NotifyPropertyChanged("DeviceGuid"); } }
#endregion
#region Presentation related
Try this (works on Silverlight too): Create a converter class:
public sealed class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
try
{
return new BitmapImage(new Uri((string)value));
}
catch
{
return new BitmapImage();
}
}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); }
}
In your XAML you must add a new resource and implement a convert in your image element inside the CellTemplate. Remember to add the extension namespace on your xaml file header.
Hope its helps :-D
Christian Amado MCITP | MCTS | MOS | MTA DCE 0★ 1★ 2★ 3★ 4★ 5★ Bronze level MVA Olimpia ☆ ★★★
-
Try this (works on Silverlight too): Create a converter class:
public sealed class ImageConverter : IValueConverter
{
public object Convert(object value, Type targetType,
object parameter, CultureInfo culture)
{
try
{
return new BitmapImage(new Uri((string)value));
}
catch
{
return new BitmapImage();
}
}public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new NotImplementedException(); }
}
In your XAML you must add a new resource and implement a convert in your image element inside the CellTemplate. Remember to add the extension namespace on your xaml file header.
Hope its helps :-D
Christian Amado MCITP | MCTS | MOS | MTA DCE 0★ 1★ 2★ 3★ 4★ 5★ Bronze level MVA Olimpia ☆ ★★★
I found the following as a source of help towards the solution:
public class ImageConverter : IValueConverter
{
public object Convert(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new BitmapImage(new Uri((string)value, UriKind.RelativeOrAbsolute));
}public object ConvertBack(object value, System.Type targetType, object parameter, System.Globalization.CultureInfo culture) { throw new System.NotImplementedException(); } }
The path to the image is Images\File.bmp. I tried changing the formatting of the string to create a proper Uri, but the image STILL does not appear in the cell X| I found a solution which helped in better creating the path to be in the proper form of a Uri. What follows IS the solution:
public class ImageConverter : IValueConverter
{public static BitmapImage CreateBMImage(string path) { BitmapImage bi = new BitmapImage(); try { bi.BeginInit(); bi.CacheOption = BitmapCacheOption.OnDemand; Uri baseUri = new Uri(Application.ResourceAssembly.Location); bi.UriSource = new Uri(baseUri, path); bi.EndInit(); } catch { return null; } return bi; } public static Image CreateImage(string path) { Image img = new Image(); img.Source = CreateBMImage(path); return img; } public object Convert(object value, Type targetType, object parameter, CultureInfo culture) { try { return CreateBMImage((string)value); } catch { return new BitmapImage(); } } public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) { throw new System.NotImplementedException(); }
}