views:

193

answers:

1

i've got a INotifyPropertyChanged-abled class and thought it would be a good idea to use:

<Image Source="{Binding myfilename, StringFormat='FixedPath/{0}.png'}" />

so whenever i would change myfilename in source, i'd get the corresponding image in my wpf gui.

it compiles. but in the console i get the error that a TargetDefaultValueConverter converter failed to convert the value of myfilename. binding works ok. only the stringformat seems not to be applied.

what am i missing here?

A: 

Disclaimer: this is somewhat conjecture

Based on some reading, that error occurs when the built-in converter fails to provide the correct type. So, what you're trying to do would be fine if the thing you're binding to expected a string. However, the Source property is actually of type BitmapSource - and for some reason, WPF is ok converting a raw string to a BitmapSource but because the target type isn't a string it is not ok running the built-in string formatter.

You could try making your own ValueConverter that does exactly this formatting.

JustABill
You're correct about the type mismatch causing the error message. Additionally the StringFormat argument is only applied to non-string types so even if it was being assigned to a string property it would still not add the formatting. A custom converter is the way to go.
John Bowen
Thanks for the clarification, that's a rather bizarre limitation.
JustABill