验证

发布时间 2023-10-19 18:31:18作者: zhangxiuju

1.Exception

<TextBox Width="120" Text="{Binding Name,ValidatesOnExceptions=True,UpdateSourceTrigger=PropertyChanged}"
    ToolTip="{Binding  RelativeSource={x:Static RelativeSource.Self}, Path=(Validation.Errors)[0].ErrorContent}">
</TextBox>

private string name;
public string Name
{    
    get
    {
        return name;
    }
    set
   {
        name = value;
        if (name.Length > 10)
        {
            throw new ArgumentException("输入超长!");
        }
   }
}

2.ValidationRule

3.IDataErrorInfo

4.INotifyDataErrorInfo

5. 代码直接验证(直接用 MessageBox  弹出消息,Windows 的老传统)