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

后端 未结 1 1747
不知归路
不知归路 2020-12-20 07:29

I\'m creating an Image.Source-String binding in code like:

var newBinding = new System.Windows.Data.Binding()
  {
    Path = new Pr         


        
相关标签:
1条回答
  • 2020-12-20 08:05

    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.

    0 讨论(0)
提交回复
热议问题