Converting byte[] to ImageSource in PropertyEditor

Grids for WPF Forum

Posted 4 years ago by Procam
Version: 19.1.0683
Avatar

Hello,

I have a class with property Thumbnail of type Byte[]. An instance of this class is set as DataObject for PropertyGrid.

In the PropertyEditor for the PropertyGrid I have:

<grids:PropertyEditor PropertyName="Thumbnail">
            <grids:PropertyEditor.ValueTemplate>
                <DataTemplate>
                    <Grid Background="White" Height="75">
                        <Image Source="{Binding Value, Converter={StaticResource ByteToImageSourceConverter}, UpdateSourceTrigger=PropertyChanged}" RenderOptions.BitmapScalingMode="NearestNeighbor" RenderOptions.EdgeMode="Aliased"></Image>
                    </Grid>

where the ByteToImageSourceConverter returns:

BitmapSource bitmap = (BitmapSource)new ImageSourceConverter().ConvertFrom(bytes);
return bitmap;

The result in the Property grid is that apart of the Image, there are also below the image the bytes enumerated as [0], [1], etc.

Hot to get rid of these "byte" items in property grid?

Comments (3)

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hello,

Yes that is the correct way to make a custom property editor to show the Image.  

The other issue you mentioned is because you have a byte array, our data factory is defaulting to making the property expandable so you can see the items.  I believe if you put this attribute on the property, it will prevent that:

[TypeConverter(typeof(TypeConverter))]


Actipro Software Support

Posted 4 years ago by Procam
Avatar

Thanks for your quick reply. I added the TypeConverter (as below) but have still the expanded [0],[1],... items.

private Byte[] thumbnail;
[TypeConverter(typeof(TypeConverter))]
[Category("Graphic"), DisplayName("Thumbnail"), Display(Order = 99)]
public Byte[] Thumbnail
{
    get => thumbnail;
    set => SetProperty(ref thumbnail, value);
}

Or maybe need I a special converter?

Posted 4 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar

Hmm, when we added that generic TypeConverter to our own test, it seemed to work since it would prevent the default ArrayConverter from being used.  ArrayConverter overrides the GetProperties and GetPropertiesSupported methods to return child properties, which are the items.  You could try making your own TypeConverter that returns false from GetPropertiesSupported and see if that helps.

Other than that, the other way would be to inherit our default data factory class and make one of your own.  Then override its methods to filter out child properties when properties that return byte arrays.


Actipro Software Support

The latest build of this product (v24.1.2) was released 4 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.