【发布时间】:2014-08-21 12:16:26
【问题描述】:
如果我希望在用户在文本字段中输入内容而不是在弹出窗口时运行验证,我需要做什么。这是示例图片:
在我执行此窗口时的示例图像中,它会自动验证。我希望它在我输入内容时检查验证。
这是我在验证中的代码 sn-p:
public string Error
{
get { return null; }
}
public string this[string propertyName]
{
get
{
string error = string.Empty;
switch (propertyName)
{
case "Name":
if (string.IsNullOrEmpty(Name))
error = "Name is required!";
break;
case "Url":
if (string.IsNullOrEmpty(Url))
error = "Url is required!";
else if (!Regex.IsMatch(Url, @"(?:https?:\/\/)?(?:[\w]+\.)([a-zA-Z\.]{2,6})([\/\w\.-]*)*\/?"))
error = "Url is invalid";
break;
case "Price":
if (Price < 0)
error = "Price cannot be negative!";
break;
default:
break;
}
return error;
}
}
这是我 UI 中的代码 sn-p:
<!--Product Name-->
<Label Content="Name:" />
<TextBox x:Name="txtName"
Grid.Column="2"
Validation.Error="ValidationError"
Text="{Binding Name,
Mode=TwoWay,
ValidatesOnDataErrors=True,
NotifyOnValidationError=True,
UpdateSourceTrigger=PropertyChanged}" />
<!--Product Url-->
<Label Grid.Row="2" Content="Url:" />
<TextBox x:Name="txtUrl"
Grid.Row="2"
Grid.Column="2"
Validation.Error="ValidationError"
Text="{Binding Url,
Mode=TwoWay,
ValidatesOnDataErrors=True,
NotifyOnValidationError=True,
UpdateSourceTrigger=PropertyChanged}"/>
【问题讨论】:
标签: c# wpf validation telerik radwindow