为什么我的TypeConverter没有被调用?

时间:2009-04-23 16:24:19

标签: wpf typeconverter

我已将TypeConverterAttribute属性应用于WPF FrameworkElement子类上的属性。该属性的类型为BitmapSource。

但是从不创建或调用TypeConverter。这是因为在ImageSource上指定的TypeConverter优先吗?

我正在尝试调试数据绑定问题。错误消息,从未提及我的TypeConverter类,建议将属性添加到属性没有任何效果:

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)'

2 个答案:

答案 0 :(得分:2)

我之前反对过这个并且忽略了它,因为我很幸运拥有了被转换的课程。但是,我刚试过这个并且可以确认 。也就是说,它确实似乎工作。

我在according to MSDN上提出了这个问题,我也提供了解决方法。

答案 1 :(得分:-2)

你必须在xaml中创建转换对象才能在绑定中使用它。查看this article

<local:MyConverter key="myconverterorsomething"/>

<TextBlock Text="{Binding Path=TimeOfSale, 
  Converter={StaticResource myconverterorsomething}, 
  ConverterParameter=' \{0:dd-MMM-yyyy hh:mm\}'}" />