views:

796

answers:

2

I've applied a TypeConverterAttribute attribute to a property on my WPF FrameworkElement subclass. The property is of type BitmapSource.

But the TypeConverter is never created or called. Is this because the TypeConverter specified on ImageSource takes precedence?

I'm trying to debug a data binding problem. The error message, never makes mention of my TypeConverter class, suggesting that adding the attribute to the property has had no effect:

System.Windows.Data Error: 6 : 'TargetDefaultValueConverter' converter failed to convert value '../SmallItemIcons/Gallery.png' (type 'String'); fallback value will be used, if available. BindingExpression:Path=SmallImage.ImageUri; DataItem='ItemSettings' (HashCode=32104365); target element is 'Bitmap' (Name=''); target property is 'Source' (type 'BitmapSource') IOException:'System.IO.IOException: Cannot locate resource 'smallitemicons/gallery.png'.
   at MS.Internal.AppModel.ResourcePart.GetStreamCore(FileMode mode, FileAccess access)
   at System.IO.Packaging.PackagePart.GetStream(FileMode mode, FileAccess access)
   at System.IO.Packaging.PackWebResponse.CachedResponse.GetResponseStream()
   at System.IO.Packaging.PackWebResponse.GetResponseStream()
   at System.IO.Packaging.PackWebResponse.get_ContentType()
   at System.Windows.Media.Imaging.BitmapDecoder.SetupDecoderFromUriOrStream(Uri uri, Stream stream, BitmapCacheOption cacheOption, Guid& clsId, Boolean& isOriginalWritable, Stream& uriStream, UnmanagedMemoryStream& unmanagedMemoryStream, SafeFileHandle& safeFilehandle)
   at System.Windows.Media.Imaging.BitmapDecoder.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy, Boolean insertInDecoderCache)
   at System.Windows.Media.Imaging.BitmapFrame.CreateFromUriOrStream(Uri baseUri, Uri uri, Stream stream, BitmapCreateOptions createOptions, BitmapCacheOption cacheOption, RequestCachePolicy uriCachePolicy)
   at System.Windows.Media.ImageSourceConverter.ConvertFrom(ITypeDescriptorContext context, CultureInfo culture, Object value)
   at MS.Internal.Data.DefaultValueConverter.ConvertHelper(Object o, Type destinationType, DependencyObject targetElement, CultureInfo culture, Boolean isForward)
   at MS.Internal.Data.TargetDefaultValueConverter.Convert(Object o, Type type, Object parameter, CultureInfo culture)
   at System.Windows.Data.BindingExpression.ConvertHelper(IValueConverter converter, Object value, Type targetType, Object parameter, CultureInfo culture)'
A: 

you have to create the convert object in the xaml to use it in your binding. Check out this article

<local:MyConverter key="myconverterorsomething"/>

<TextBlock Text="{Binding Path=TimeOfSale, 
  Converter={StaticResource myconverterorsomething}, 
  ConverterParameter=' \{0:dd-MMM-yyyy hh:mm\}'}" />
Muad'Dib
That's a value converter. I am trying to debug the TypeConverter stage, so am trying to set the TypeConverter.
mackenir
MSDN example: http://msdn.microsoft.com/en-us/library/bb546926.aspx
Muad'Dib
That's an example where the TypeConverter attrib is attached to the type being converted rather than the property - I read somewhere that you could attach the attrib to the property. This latter usage is what I'm trying to do.
mackenir
(Since I can't rewrite the BitmapSource class)
mackenir
+2  A: 

I've come up against this before and ignored it because I had the luxury of owning the class being converted. However, I just tried this and can confirm that according to MSDN it should work. That said, it certainly seems that it does not work.

I raised this on the WPF Disciples forum, where I also supply a workaround.

HTH, Kent

Kent Boogaart
Thanks, I'll keep an eye on that thread. In fact I'll keep an eye on that forum!NB: The second link should be http://groups.google.com/group/wpf-disciples/browse_thread/thread/9f7bb40b7413fcd
mackenir
Link is fixed now - thanks.
Kent Boogaart