Bound image not displaying in item

2 posts, 0 answers
  1. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 05 Feb 2010 Link to this post

    I created a hierachial datasource that I am binding to for the TreeView.  Everything works except that one of the items I bind to is an image column and nothing is showing up.  I am using Ria and the data is MS SQL Sever.  The image format is a PNG (not gif) and the data is coming back fine.  Here is how I have my template:
    <DataTemplate x:Key="SymbolInfo">  
                <StackPanel Orientation="Horizontal">  
                    <Image  Source="{Binding Path=Image}" Margin=" 0,0,6,0"/>  
                    <TextBlock Text="{Binding Label}" Foreground="Blue" FontWeight="Bold" FontSize="9" /> 
                </StackPanel> 
            </DataTemplate> 
    The TextBlock is binding with Label column, but not the image.  Any help is appreciated.

    Thanks,
    Hector
  2. Hector
    Hector avatar
    92 posts
    Member since:
    May 2009

    Posted 10 Feb 2010 Link to this post

    I ended up using a converter for this.  Here is the post with the code.
    <DataTemplate x:Key="SymbolInfo">     
                <StackPanel Orientation="Horizontal" Background="AliceBlue">     
                    <Image  Source="{Binding Image, Converter={StaticResource ImageConverter}}" Stretch="Fill" Loaded="Image_Loaded"/>     
                    <TextBlock Text="{Binding Label}" Foreground="Blue" FontWeight="Bold" FontSize="9" />    
                </StackPanel>    
            </DataTemplate>   
     

    converter:
    public class ImageByteConverter : IValueConverter  
        {  
            public static object ByteToBitmap(byte[] byteImg)  
            {  
                using (MemoryStream ms = new MemoryStream(byteImg, 0, byteImg.Length))  
                {  
                    //Convert byte[] to image  
                    Image image = new Image();  
                    BitmapImage bitmapimage = new BitmapImage();  
                    MemoryStream stream = new MemoryStream(byteImg);  
                    bitmapimage.SetSource(stream);  
                    return bitmapimage;  
                }  
            }  
     
            public object Convert(object value, Type targetType, object parameter, CultureInfo culture)  
            {  
                byte[] byteImg = (byte[])value;  
                //Convert byte to image  
                return ByteToBitmap(byteImg);  
            }  
     
              
     
            public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)  
            {  
                throw new NotImplementedException();  
            }  
     
        }  
Back to Top