如何在Image.Source上显示验证错误?

时间:2009-05-13 11:36:25

标签: wpf validation data-binding binding

我将Image的Source属性绑定到模型上的URI字符串属性:

<Image Validation.ErrorTemplate="{StaticResource validationTemplate}">
  <Image.Source>
    <Binding Path="LargeImage.ImageUri">
      <Binding.ValidationRules>
        <ExceptionValidationRule/>
      </Binding.ValidationRules>
    </Binding>
  </Image.Source>
</Image>

<ControlTemplate x:Key="validationTemplate">
  <Border BorderThickness="2" CornerRadius="2" BorderBrush="Red">
    <AdornedElementPlaceholder/>
  </Border>
</ControlTemplate>

当LargeImage.ImageUri不是有效图像时,我希望图像显示红色边框,但这不会发生。

这是因为问题在于转换绑定值而不是设置它吗?

我可以看到抛出将字符串ImageUri转换为ImageSource的异常:

System.Windows.Data Error: 18 : Cannot convert 'C:\not-an-image.txt' from type 'String' to type 'System.Windows.Media.ImageSource' for 'en-US' culture with default conversions; consider using Converter property of Binding. NotSupportedException:'System.NotSupportedException: No imaging component suitable to complete this operation was found. ---> System.Runtime.InteropServices.COMException (0x88982F50): Exception from HRESULT: 0x88982F50
   --- End of inner exception stack trace ---
   at MS.Internal.HRESULT.Check(Int32 hr)
   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)'
System.Windows.Data Error: 6 : 'TargetDefaultValueConverter' converter failed to convert value 'C:\not-an-image.txt' (type 'String'); fallback value will be used, if available. BindingExpression:Path=LargeImage.ImageUri; DataItem='ItemSettings' (HashCode=60569775); target element is 'Image' (Name=''); target property is 'Source' (type 'ImageSource') NotSupportedException:'System.NotSupportedException: No imaging component suitable to complete this operation was found. ---> System.Runtime.InteropServices.COMException (0x88982F50): Exception from HRESULT: 0x88982F50
   --- End of inner exception stack trace ---
   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)'

1 个答案:

答案 0 :(得分:2)

  

这是因为问题在于   转换绑定值,而不是   比设定它?

在某种程度上,如果您的意思是设置/更新绑定源而不是绑定目标,请参阅ExceptionValidationRule的MSDN说明:

  

表示检查的规则   在...期间抛出的异常   更新绑定源属性。

突出显示的短语在此处是关键:规则适用于更新源而非目标的绑定操作。在你的情况下,Image.Source是绑定目标,而LargeImage.ImageUri是绑定源,因此反过来关于你想要实现的目标。

基本上,您希望验证现有模型而不是用户输入(规范示例是接受用户输入并应用某些约束的TextBox,例如字符串长度或整数范围)。但是,通过ValidationRule Class WPF数据绑定仅针对后一种情况:

  

提供创建自定义规则的方法   为了检查用户的有效性   输入

有关详细信息,请参阅Data Validation中的Data Binding Overview部分。