0
A
回答
2
您可以为一个TextBox
<Style x:Key="TextBoxWithValidation" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource TextBoxValidationTemplate}"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="{StaticResource BackgroundValidationBrush}"/>
<Setter
Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self},
Path=(Validation.Errors)[0].ErrorContent)}"
/>
</Trigger>
</Style.Triggers>
</Style>
凡BackgroundValidationBrush会说粉红色做这样的事情。
请注意,在没有错误的情况下,ToolTip绑定到(Validation.Errors)[0] .ErrorContent的常见解决方案将导致大量调试问题(技术术语),所以最好使用类似转换器这样的:
[ValueConversion(typeof(ReadOnlyObservableCollection<ValidationError>), typeof(string))]
public class ValidationErrorsToStringConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter,
CultureInfo culture)
{
var errors = value as ReadOnlyObservableCollection<ValidationError>;
// If there are no errors then return an empty string.
// This prevents debug exception messages that result from the usual Xaml of "Path=(Validation.Errors)[0].ErrorContent".
// Instead we use "Path=(Validation.Errors), Converter={StaticResource ValidationErrorsConverter}".
if (errors == null)
{
return string.Empty;
}
var errors2 = errors.Select(e => e.ErrorContent).OfType<string>().ToArray();
return errors.Any() ? string.Join("\n", errors2) : string.Empty;
}
public object ConvertBack(object value, Type targetType, object parameter,
CultureInfo culture)
{
throw new NotImplementedException();
}
}
和我们可以使用
<converters:ValidationErrorsToStringConverter x:Key="ValidationErrorsConverter"/>
<!-- Style to be used as the base style for all text boxes -->
<Style x:Key="TextBoxWithValidation" TargetType="{x:Type TextBox}" BasedOn="{StaticResource {x:Type TextBox}}">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource TextBoxValidationTemplate}"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="Background" Value="{StaticResource BackgroundValidationBrush}"/>
<Setter
Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Mode=Self},
Path=(Validation.Errors),
Converter={StaticResource ValidationErrorsConverter}}"
/>
</Trigger>
</Style.Triggers>
</Style>
0
我想你寻找的东西,像
能否使用sniplet:
<Binding.ValidationRules>
<DataErrorValidationRule/>
</Binding.ValidationRules>
定义的验证规则,并在出现故障的情况下,你会得到一个红色矩形 角落找寻相关的控制。
0
将附加属性Validaton.ErrorTemplate
设置为您的TextBox
样式。 (见一个例子文档)
相关问题
- 1. 在Javascript中设置多样式属性时出现错误
- 2. 错误出现时的样式DropdownListFor
- 3. 设置django_language Cookie时出现LocaleMiddleware错误
- 4. 当我尝试为Label设置文本时出现错误?
- 5. 如何在设置satchmo时出现模板语法错误?
- 6. 使用DateTime设置日期时出现Symfony2格式错误
- 7. Android:EditText不会以编程方式设置样式时出现
- 8. 设置Uri时出现错误的位图错误
- 9. 当跑步流浪时出现错误
- 10. 当设置表单值时,JQuery val()抛出运行时错误
- 11. 在设置innerhtml时出现IE未知运行时错误
- 12. 数组设置抛出启动时出现错误 - CS0021错误(C#)
- 13. 尝试使用JavaScript设置样式时出错
- 14. 当AsyncRendering设置为false时ReportViewer错误
- 15. 当通过json undefined成员时,配置javascript会出现错误?
- 16. 配置LDAP时出现Tomcat错误
- 17. 当调用方法绑定时出现错误形式
- 18. 在模式窗口中显示错误,当出现问题时
- 19. 当我使用AVPlayerLayer时出现错误方式的放大镜
- 20. 尝试设置$ JAVA_HOME时出错 - 错误:未设置JAVA_HOME
- 21. 计算公式时出现Java错误
- 22. 如何设置VS2015当我搞乱我的Javascript时抛出错误?
- 23. 创建表单时出现“对象引用未设置”错误
- 24. 设置对象变量时出现错误IE.Document.getElement
- 25. 在AWS上设置分析服务器时出现错误
- 26. 为什么在设置PhantomJS驱动程序时出现错误?
- 27. 在Windows 7上设置MONGODB路径时出现错误
- 28. 设置上下文映射时出现网络错误 - IBM Bluemix
- 29. 在WCF中设置ClientCredentials时出现“Object is read only”错误
- 30. 在edittext上设置日历时出现错误onclick
我只看到传递给我的一个转换器的错误信息,即使一个以上的ViewModel's.GetErrors(“属性名”)存在...想法? – gap 2016-06-23 20:32:32