wpf图像来源:“格式”使用字符串格式绑定(-> 文件名)
我有一个 INotifyPropertyChanged-abled 类,并认为使用它是一个好主意:
<Image Source="{Binding myfilename, StringFormat='FixedPath/{0}.png'}" />
所以每当我在源中更改我的文件名时,我都会在我的 wpf gui 中获得相应的图像。
它编译。但在控制台中,我收到错误消息:TargetDefaultValueConverter 转换器无法转换 myfilename 的值。绑定工作正常。只有字符串格式似乎不适用。
我在这里缺少什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
免责声明:这在某种程度上是推测,
根据一些阅读,当内置转换器无法提供正确的类型时,就会发生该错误。因此,如果您要绑定的内容需要一个
字符串
,那么您尝试做的事情就很好。但是,Source
属性实际上是BitmapSource
类型 - 并且出于某种原因,WPF 可以将原始string
转换为BitmapSource
但由于目标类型不是string
,因此无法运行内置字符串格式化程序。您可以尝试制作自己的 ValueConverter 来完成这种格式设置。
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, theSource
property is actually of typeBitmapSource
- and for some reason, WPF is ok converting a rawstring
to aBitmapSource
but because the target type isn't astring
it is not ok running the built-in string formatter.You could try making your own ValueConverter that does exactly this formatting.