How does Xaml create the string to BitmapImage value conversion when binding to Image.Source?

徘徊边缘 提交于 2019-11-29 12:44:57
Will

System.Windows.Media.ImageSource has a TypeConverterAttribute

[TypeConverter(typeof(ImageSourceConverter))]

The binding will look for this and use the converter automatically.

If you look at the ImageSourceConverter you can see what types it can convert from:

if (sourceType == typeof(string) || 
    sourceType == typeof(Stream) || 
    sourceType == typeof(Uri) || 
    sourceType == typeof(byte[]))
{
    return true;
}

In order to mimic this process, you must add a TypeConverterAttribute on the Type of the property being bound to.

You can do this by 1. controlling the type, or 2. use the TypeDescriptor at runtime to add the attribute. There's a question about this here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!