This is a migrated thread and some comments may be shown as answers.

Bound image not displaying in item

1 Answer 105 Views
TreeView
This is a migrated thread and some comments may be shown as answers.
Hector
Top achievements
Rank 1
Hector asked on 05 Feb 2010, 11:54 PM
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

1 Answer, 1 is accepted

Sort by
0
Hector
Top achievements
Rank 1
answered on 10 Feb 2010, 01:41 PM
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();  
        }  
 
    }  
Tags
TreeView
Asked by
Hector
Top achievements
Rank 1
Answers by
Hector
Top achievements
Rank 1
Share this question
or