在 WPF 中的不同控件上显示验证错误模板 [英] Show the validation error template on a different control in WPF

查看:31
本文介绍了在 WPF 中的不同控件上显示验证错误模板的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 UserControl 包含其他控件和一个 TextBox.它有一个 Value 属性,该属性绑定到 TextBox 文本并且将 ValidatesOnDataErrors 设置为 True.

I have a UserControl that contains other controls and a TextBox. It has a Value property that is bound to the TextBox text and has ValidatesOnDataErrors set to True.

Value 属性绑定中发生验证错误时,错误模板(标准红色边框)会显示在整个 UserControl 周围.

When a validation error occurs in the Value property binding, the error template (standard red border) is shown around the entire UserControl.

有没有办法只在 TextBox 周围显示它?我希望能够使用任何错误模板,所以简单地在文本框周围放置边框并将其颜色或其他内容绑定到 Validation.HasError 不是一种选择.

Is there a way to show it around the TextBox only? I'd like to be able to use any error template so simply putting border around textbox and binding its color or something to Validation.HasError is not an option.

这是我的代码:

<DataTemplate x:Key="TextFieldDataTemplate">
    <c:TextField DisplayName="{Binding Name}" Value="{Binding Value, Mode=TwoWay, ValidatesOnDataErrors=True}"/>
</DataTemplate>

<controls:FieldBase x:Name="root">
<DockPanel DataContext="{Binding ElementName=root}">
    <TextBlock Text="{Binding DisplayName}"/>
    <TextBox x:Name="txtBox"                 
             Text="{Binding Value, Mode=TwoWay, ValidatesOnDataErrors=True}"
             IsReadOnly="{Binding IsReadOnly}"/>
</DockPanel>

UserControl (FieldBase) 然后绑定到执行验证的 ModelView.

UserControl (FieldBase) is than bound to ModelView which performs validation.

推荐答案

为了完成这个任务,我使用了这个解决方案.它使用转换器,通过将 (Validation.Errors).CurrentItem 转换为厚度来隐藏"边框.

to accomplish this task I've used this solution. It uses converter, that "hides" border by converting (Validation.Errors).CurrentItem to Thickness.

<Grid>
    <Grid.Resources>
        <data:ValidationBorderConverter
            x:Key="ValidationBorderConverter" />
    </Grid.Resources>
    <Border
        BorderBrush="#ff0000"
        BorderThickness="{Binding 
            ElementName=myControl, 
            Path=(Validation.Errors).CurrentItem, 
            onverter={StaticResource ValidationBorderConverter}}">
        <TextBox
            ToolTip="{Binding 
                ElementName=myControl, 
                Path=(Validation.Errors).CurrentItem.ErrorContent}" />
    </Border>
</Grid>

ValidationBorderConverter 类非常简单:

ValidationBorderConverter class is pretty simple:

[ValueConversion(typeof(object), typeof(ValidationError))]
public sealed class ValidationBorderConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, 
            System.Globalization.CultureInfo culture)
    {
        return (value == null) ? new Thickness(0) : new Thickness(1);
    }

    public object ConvertBack(object value, Type targetType, object parameter, 
            System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

这篇关于在 WPF 中的不同控件上显示验证错误模板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆