开发者

wpf image source: "format" binding (-> filename) with stringformat

开发者 https://www.devze.com 2023-01-02 03:31 出处:网络
i\'ve got a开发者_如何转开发 INotifyPropertyChanged-abled class and thought it would be a good idea to use:

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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消