WPF 验证文本框,绑定应该是什么样子?
我正在尝试验证 WPF 中的文本框。我在互联网上找到了一些例子,并整理了一些内容。但现在它只是验证一切,就好像它是错误的一样。有人告诉我这是我的绑定,因为我不确定要绑定什么内容,我来这里是为了澄清:)
这是我的验证器:
class TextRangeValidator : ValidationRule
{
private int _minimumLength = 0;
private int _maximumLength = 0;
private string _errorMessage;
public int MinimumLength
{
get { return _minimumLength; }
set { _minimumLength = value; }
}
public int MaximumLength
{
get { return _maximumLength; }
set { _maximumLength = value; }
}
public string ErrorMessage
{
get { return _errorMessage; }
set { _errorMessage = value; }
}
public override ValidationResult Validate(object value,
CultureInfo cultureInfo)
{
ValidationResult result = new ValidationResult(true, null);
string inputString = (string)value.ToString();
if (inputString.Length < this.MinimumLength ||
(
inputString.Length > this.MaximumLength))
{
result = new ValidationResult(false, this.ErrorMessage);
}
return result;
}
}
这是我的 xaml 代码:
<TextBox Height="23" HorizontalAlignment="Left" Margin="118,60,0,0" Name="CreateUserCPRTextbox" VerticalAlignment="Top" Width="120" >
<TextBox.Text >
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="true">
<Binding.ValidationRules >
<validators:TextRangeValidator
ValidatesOnTargetUpdated="True"
MinimumLength="10"
MaximumLength="10"
ErrorMessage="CPR nummer ikke gyldigt" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
现在我的绑定属性只是命名为 name,我已经尝试将其绑定到 CreateUserCPRTextbox.Text 但它不起作用。这是如何运作的?
I'm trying to validate a textbox in WPF. I found some examples on the internet and I've put together some things. But right now it just validates everything as if it was wrong. I was told it's my binding, as I'm not sure what to bind to what I've come here for some clarification :)
Here's my validator:
class TextRangeValidator : ValidationRule
{
private int _minimumLength = 0;
private int _maximumLength = 0;
private string _errorMessage;
public int MinimumLength
{
get { return _minimumLength; }
set { _minimumLength = value; }
}
public int MaximumLength
{
get { return _maximumLength; }
set { _maximumLength = value; }
}
public string ErrorMessage
{
get { return _errorMessage; }
set { _errorMessage = value; }
}
public override ValidationResult Validate(object value,
CultureInfo cultureInfo)
{
ValidationResult result = new ValidationResult(true, null);
string inputString = (string)value.ToString();
if (inputString.Length < this.MinimumLength ||
(
inputString.Length > this.MaximumLength))
{
result = new ValidationResult(false, this.ErrorMessage);
}
return result;
}
}
Here's my xaml code:
<TextBox Height="23" HorizontalAlignment="Left" Margin="118,60,0,0" Name="CreateUserCPRTextbox" VerticalAlignment="Top" Width="120" >
<TextBox.Text >
<Binding Path="Name" UpdateSourceTrigger="PropertyChanged" ValidatesOnDataErrors="true">
<Binding.ValidationRules >
<validators:TextRangeValidator
ValidatesOnTargetUpdated="True"
MinimumLength="10"
MaximumLength="10"
ErrorMessage="CPR nummer ikke gyldigt" />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
Right now my binding property is just named name, I've tried to make it bind to the CreateUserCPRTextbox.Text but it doesn't work. How does this work?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将其绑定到源和路径,例如以下简单对象:
可以通过将其添加到窗口的资源集合来创建该对象。
源绑定到对象及其属性 [Name] 的路径,在本例中为“0123456789”。
最后,您必须通过 Source={StaticResource class1} 将其源绑定到此资源。
接下来,您可以使用经过验证的文本框播放一些内容。
You have to bind it to a source and path, for example the following simple object:
The object can be created by adding it to the window its resource collection.
The source binds to the object and the path to its property [Name], which in this example is "0123456789".
Lastly, you have to bind its source to this resource by Source={StaticResource class1}
Next, you can play some with the validated textbox.