2

我是 WPF 新手,并试图在提交表单上实现验证控制。

谁能帮我。即使我输入了无效数据,我的代码也不会显示任何错误消息,它什么也不做。

这是我的代码,

 public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
    }
}


public class UserName : INotifyPropertyChanged, IDataErrorInfo
{
    public event PropertyChangedEventHandler PropertyChanged;
    public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        if (PropertyChanged != null)
            PropertyChanged(this, e);
    }

    private string username;
    public string _UserName
    {
        get { return username; }
        set
        {
            username = value;
            OnPropertyChanged(new PropertyChangedEventArgs("_UserName"));
        }
    }
    public string this[string propertyName]
    {
        get
        {
            if (propertyName == "_UserName")
            {
                bool valid = true;
                foreach (char c in _UserName)
                {
                    if (!Char.IsLetterOrDigit(c))
                    {
                        valid = false;
                        break;
                    }
                }
                if (!valid)
                    return "The Username can only contain letters and numbers.";
            }
            return null;
        }
    }
    public string Error
    {
        get { return null; }
    }
}

我的 XAML 代码是,

<Grid>
    <Label Content="User Name" Height="28" HorizontalAlignment="Left" Margin="27,37,0,0" Name="UserNameLB" VerticalAlignment="Top" Width="96" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="135,37,0,0" Name="UserNameTB" VerticalAlignment="Top" Width="189">
        <TextBox.Text>
            <Binding Path="_UserName">
                <Binding.ValidationRules>
                    <DataErrorValidationRule></DataErrorValidationRule>
                </Binding.ValidationRules>
            </Binding>
        </TextBox.Text>
    </TextBox>

</Grid>
4

2 回答 2

1

您所需要做的就是将有关验证的所有逻辑放在一个单独的类中,该类继承自 classValidationRule和 override method Validate。然后你可以设置你想看到的关于验证失败原因的消息,比如:

public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
    try
    {
        if (!Char.IsLetterOrDigit(c))
        {
           return new ValidationResult(false,
                                       "The Username can only contain letters and numbers.");
        }
        else
        {
            return new ValidationResult(true, null);
        }
    }
    catch (Exception e)
    {
        return new ValidationResult(false, "Illegal characters or " + e.Message);
    }
}
于 2018-07-19T10:04:55.877 回答
0

试试这个:

编辑:(这是我定义的显示所有TextBox控件错误的样式)(将其放入Window.Resources

然后,此样式将显示错误消息ToolTip

<Style x:Key="{x:Type TextBox}" TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="true">
            <Setter Property="ToolTip"
                    Value="{Binding RelativeSource={x:Static RelativeSource.Self},
                    Path=(Validation.Errors)[0].ErrorContent}"/>
        </Trigger>
    </Style.Triggers>
</Style>

<Grid>
    <Label Content="User Name" Height="28" HorizontalAlignment="Left" Margin="27,37,0,0" Name="UserNameLB" VerticalAlignment="Top" Width="96" />
    <TextBox Height="23" HorizontalAlignment="Left" Margin="135,37,0,0" 
             Name="UserNameTB" VerticalAlignment="Top" Width="189"
             Text={Binding Path=_UserName, UpdateSourceTrigger=LostFocus, 
                   ValidatesOnDataErrors=true, NotifyOnValidationError=true} />    
</Grid>

来源

于 2012-01-13T16:11:18.093 回答