1

For some reason, I am getting the assembly name for every time I am trying to bind to an Image. I am getting System.Windows.Control.Image in my TextBlock rather than the image itself.

My XAML looks like this

<TextBlock FontSize="16">
     <TextBlock.Text>
        <MultiBinding StringFormat=" {0} {1}">
           <Binding Path="Icon"></Binding>
           <Binding Path="Name"></Binding>
          </MultiBinding>
      </TextBlock.Text>
</TextBlock> 

And in my Model class, I am creating an Image like this:

public Image Icon
{
   get
   {
       if (isFolder)
       {
           Image folderImage = new Image();
           BitmapImage logo = new BitmapImage();
           logo.BeginInit();
           logo.UriSource = new Uri("pack://application:,,,/ComputerProject;component/Resources/FolderIcon.jpg");
           logo.EndInit();
           folderImage.Source = logo;
           return folderImage;
       }
       else
       {
            return new Image(); //TODO
       }
   }
}

Can this be done in a TextBlock? I have tried using multiple textblocks rather than doing the StringFormatting but that didn't work either.

tabby
  • 1,878
  • 1
  • 21
  • 39
  • I think I did something like this in the past and one option is that you will have to use the string in the texbox and create an image resource, or create a converter (probably better) that know how to translate string to image. Something like [How do you convert a byte array to a hexadecimal string, and vice versa?](https://stackoverflow.com/questions/311165/how-do-you-convert-a-byte-array-to-a-hexadecimal-string-and-vice-versa) – rmjoia Oct 04 '17 at 14:09
  • "Can this be done in a TextBlock?" No. What are you trying to achieve? Should there be an image with some text on top? – Clemens Oct 04 '17 at 14:18
  • It's suppose to be single-line double binding. The textblock binding should be an image and then right next to it, a String Name label. –  Oct 04 '17 at 14:20
  • That's not what MultiBinding is supposed to do. – Clemens Oct 04 '17 at 14:20
  • Sorry, made a typo. It's suppose to have a single line with two bindings within. So it should be Icon - Name and right next to each other and the Icon is an Image and Name is a String. –  Oct 04 '17 at 14:21

1 Answers1

2

Simply use an Image and a TextBlock element:

<StackPanel Orientation="Horizontal">
    <Image Source="{Binding Icon}"/>
    <TextBlock Text="{Binding Name}"/>
</StackPanel>

where the Icon property is of type ImageSource, Uri or just string.

Example:

public ImageSource Icon
{
    get
    {
        if (isFolder)
        {
            return new BitmapImage(new Uri(
                "pack://application:,,,/ComputerProject;component/Resources/FolderIcon.jpg"));
        }

        return null; //TODO
    }
}
Clemens
  • 123,504
  • 12
  • 155
  • 268