I am binding an Image's Source property to a URI string property on the model:
我将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>
I want the Image to display a red border when LargeImage.ImageUri isn't a valid image, but this isn't happening.
当LargeImage.ImageUri不是有效图像时,我希望图像显示红色边框,但这不会发生。
Is this because the problem is with converting the bound value, rather than with setting it?
这是因为问题在于转换绑定值,而不是设置它?
I can see that an exception is thrown converting the string ImageUri to an ImageSource:
我可以看到抛出一个异常将字符串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 个解决方案
#1
Is this because the problem is with converting the bound value, rather than with setting it?
这是因为问题在于转换绑定值,而不是设置它?
In a way yes, if you mean setting/updating the binding source rather than the binding target, see the MSDN description of ExceptionValidationRule
:
在某种程度上,如果您的意思是设置/更新绑定源而不是绑定目标,请参阅ExceptionValidationRule的MSDN描述:
Represents a rule that checks for exceptions that are thrown during the update of the binding source property.
表示检查更新绑定源属性期间引发的异常的规则。
The highlighted phrase is key here: the rule applies to binding operations updating the source, not the target. In your case Image.Source is the binding target while LargeImage.ImageUri is the binding source, hence the other way round concerning what you are trying to achieve.
突出显示的短语在此处是关键:规则适用于更新源而非目标的绑定操作。在你的情况下,Image.Source是绑定目标,而LargeImage.ImageUri是绑定源,因此反过来关于你想要实现的目标。
Essentially you would like to validate the existing model rather than user input (with the canonical example being a TextBox accepting user input and applying some constraints, e.g. string length or integer range). However, by means of the ValidationRule
Class WPF data binding is only targeting the latter scenario:
基本上,您希望验证现有模型而不是用户输入(规范示例是TextBox接受用户输入并应用一些约束,例如字符串长度或整数范围)。但是,通过ValidationRule类WPF数据绑定仅针对后一种情况:
Provides a way to create a custom rule in order to check the validity of user input.
提供一种创建自定义规则的方法,以检查用户输入的有效性。
See section Data Validation within Data Binding Overview for more details on this.
有关详细信息,请参阅数据绑定概述中的数据验证部分。
#1
Is this because the problem is with converting the bound value, rather than with setting it?
这是因为问题在于转换绑定值,而不是设置它?
In a way yes, if you mean setting/updating the binding source rather than the binding target, see the MSDN description of ExceptionValidationRule
:
在某种程度上,如果您的意思是设置/更新绑定源而不是绑定目标,请参阅ExceptionValidationRule的MSDN描述:
Represents a rule that checks for exceptions that are thrown during the update of the binding source property.
表示检查更新绑定源属性期间引发的异常的规则。
The highlighted phrase is key here: the rule applies to binding operations updating the source, not the target. In your case Image.Source is the binding target while LargeImage.ImageUri is the binding source, hence the other way round concerning what you are trying to achieve.
突出显示的短语在此处是关键:规则适用于更新源而非目标的绑定操作。在你的情况下,Image.Source是绑定目标,而LargeImage.ImageUri是绑定源,因此反过来关于你想要实现的目标。
Essentially you would like to validate the existing model rather than user input (with the canonical example being a TextBox accepting user input and applying some constraints, e.g. string length or integer range). However, by means of the ValidationRule
Class WPF data binding is only targeting the latter scenario:
基本上,您希望验证现有模型而不是用户输入(规范示例是TextBox接受用户输入并应用一些约束,例如字符串长度或整数范围)。但是,通过ValidationRule类WPF数据绑定仅针对后一种情况:
Provides a way to create a custom rule in order to check the validity of user input.
提供一种创建自定义规则的方法,以检查用户输入的有效性。
See section Data Validation within Data Binding Overview for more details on this.
有关详细信息,请参阅数据绑定概述中的数据验证部分。