【问题标题】:Why is my TypeConverter not called?为什么我的 TypeConverter 没有被调用?
【发布时间】:2009-04-23 16:24:18
【问题描述】:

我已将 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)'

【问题讨论】:

    标签: wpf typeconverter


    【解决方案1】:

    我之前遇到过这个问题并忽略了它,因为我拥有拥有正在转换的课程的奢侈。但是,我刚刚尝试过,可以确认according to MSDN应该 工作。也就是说,它似乎确实不起作用

    我在 WPF Disciples forum 上提出了这个问题,我还提供了一个解决方法。

    【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 这是一个价值转换器。我正在尝试调试 TypeConverter 阶段,因此正在尝试设置 TypeConverter。
    • 这是一个示例,其中 TypeConverter 属性附加到要转换的类型而不是属性 - 我在某处读到您可以将属性附加到属性。后一种用法是我想要做的。
    • (因为我无法重写BitmapSource类)
    猜你喜欢
    • 2018-07-11
    • 1970-01-01
    • 1970-01-01
    • 2023-03-22
    • 1970-01-01
    • 2022-10-15
    • 2011-04-11
    • 2023-03-07
    • 1970-01-01
    相关资源
    最近更新 更多