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.

5
  • 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? Commented Oct 4, 2017 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? Commented Oct 4, 2017 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. Commented Oct 4, 2017 at 14:20
  • That's not what MultiBinding is supposed to do. Commented Oct 4, 2017 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. Commented Oct 4, 2017 at 14:21

1 Answer 1

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
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

This worked. Thank you. I hit the check-mark. I appreciate it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.