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
Images\10564.jpgin the resource?