1

I have a Rectangle in WPF, I can set it's Fill by using <ImageBrush ImageSource="Images\10564.jpg"/>. This is my XAML for Rectangle:

<Rectangle.Fill>
<ImageBrush ImageSource="Images\10564.jpg"/>
</Rectangle.Fill>

I want to be able to change Fill dynamically from code using bindings. Image names are stored in my database and file path and extensions are the same for all files (images).

This is what I've tried:

<ImageBrush ImageSource="{Binding Path=itemNumber, StringFormat='Images\{0}\.jpg'}"/>

But using this code above i get exception/error: 'System.Windows.Baml2006.TypeConverterMarkupExtension' threw an exception.' Line number '480' and line position '34'. I guess it has something to do with converting string to path?

Using converter everything works! Here is VB.NET class which works:

Imports System.Globalization
Public Class ImageSourceConverter
Implements IValueConverter
Private Function IValueConverter_Convert(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.Convert
    Return New BitmapImage(New Uri(String.Format("pack://application:,,,/Images/{0}.jpg", value)))
End Function

Private Function IValueConverter_ConvertBack(value As Object, targetType As Type, parameter As Object, culture As CultureInfo) As Object Implements IValueConverter.ConvertBack
    Throw New NotSupportedException()
End Function

End Class

6
  • is the Images\10564.jpg in the resource? Commented Feb 8, 2018 at 8:26
  • right click on the file from solution explorer,goto properties,change build action to RESOURCE and then debug Commented Feb 8, 2018 at 8:27
  • @zackraiyan yes it is, using "Images\10564.jpg" image loads up correctly. but binding filename shows error. All files are set to Build action: Resource Commented Feb 8, 2018 at 8:28
  • StringFormat is useless, because the target property is not a string. As already said, use a Converter. Commented Feb 8, 2018 at 8:32
  • @Clemens How would I convert string to path? Commented Feb 8, 2018 at 8:34

1 Answer 1

1

You should use a Binding Converter, which may look like this:

public class ImageSourceConverter : IValueConverter
{
    public object Convert(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        return new BitmapImage(new Uri(
            string.Format("pack://application:,,,/Images/{0}.jpg", value)));
    }

    public object ConvertBack(
        object value, Type targetType, object parameter, CultureInfo culture)
    {
        throw new NotSupportedException();
    }
}

Declare the converter as XAML resource like this:

<Window.Resources>
    <local:ImageSourceConverter x:Key="ImageSourceConverter" />
</Window.Resources>

und use it in your Binding:

<ImageBrush ImageSource="{Binding Path=itemNumber,
                                  Converter={StaticResource ImageSourceConverter}}"/>

More about using converters can be found here: https://learn.microsoft.com/en-us/dotnet/framework/wpf/data/how-to-convert-bound-data

Sign up to request clarification or add additional context in comments.

2 Comments

As a hint, when you add MSDN or MS Docs links to your post, use their up-to-date version and the en-us locale.
It works as I wanted it. Thank you so much. I will update my question with VB.NET code which works.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.