Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. WPF
  4. Adding datagrid cell images at runtime doesn't appear in cell

Adding datagrid cell images at runtime doesn't appear in cell

Scheduled Pinned Locked Moved WPF
wpfcsharpdebugginglearning
3 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • A Offline
    A Offline
    alleyes 0
    wrote on last edited by
    #1

    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 info

    private 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

    C 1 Reply Last reply
    0
    • A alleyes 0

      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 info

      private 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

      C Offline
      C Offline
      Christian Amado
      wrote on last edited by
      #2

      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 ☆ ★★★

      A 1 Reply Last reply
      0
      • C Christian Amado

        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 ☆ ★★★

        A Offline
        A Offline
        alleyes 0
        wrote on last edited by
        #3

        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();
        }
        

        }

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups