在 WPF 中使用依赖属性和样式触发器的验证
我正在尝试在 WPF 中使用验证。 我创建了一个 NotNullOrEmptyValidationRule,如下所示:
public class NotNullOrEmptyValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (String.IsNullOrEmpty(value as String))
return new ValidationResult(false, "Value cannot be null or empty");
return new ValidationResult(true, null);
}
}
现在,我需要在我的应用程序中使用它。 在我的 App.xaml 文件中,我声明了文本框的样式。 这是声明。
<Style x:Key="textBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
现在,我想在我的 TextBox 上使用它,因此我使用以下代码:
<TextBox Style="{StaticResource textBoxStyle}">
<TextBox.Text>
<Binding>
<Binding.ValidationRules>
<NotNullOrEmptyValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
错误出现在标签 NotNullOrEmptyValidationRule 上。 XAML 语法检查器无法解析 NotNullOrEmptyValidationRule。 我什至尝试过放置名称空间,但它似乎不起作用。
I am trying to use Validation in WPF. I created a NotNullOrEmptyValidationRule as shown below:
public class NotNullOrEmptyValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
if (String.IsNullOrEmpty(value as String))
return new ValidationResult(false, "Value cannot be null or empty");
return new ValidationResult(true, null);
}
}
Now, I need to use it in my application. In my App.xaml file I declared the Style for the TextBox. Here is the declaration.
<Style x:Key="textBoxStyle" TargetType="{x:Type TextBox}">
<Setter Property="Background" Value="Green"/>
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="True">
<Setter Property="Background" Value="Red"/>
<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self},Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
Now, I want to use it on my TextBox so I am using the following code:
<TextBox Style="{StaticResource textBoxStyle}">
<TextBox.Text>
<Binding>
<Binding.ValidationRules>
<NotNullOrEmptyValidationRule />
</Binding.ValidationRules>
</Binding>
</TextBox.Text>
</TextBox>
The error comes on the Tag NotNullOrEmptyValidationRule. The XAML syntax checker is not able to resolve the NotNullOrEmptyValidationRule. I have even tried putting the namespace but it does not seem to work.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您只需将 xmlns 添加到您的窗口,并使用它来引用您的 ValidationRule。
在 WPF 中,该对象完全可以在同一程序集中使用。
由于您的规则未在标准 XAML 命名空间中定义,因此您必须创建到 clr 命名空间的映射,如下所示:
然后您将像这样使用它:
编辑
我向绑定添加了一条路径语句。 你必须告诉 Binding 要绑定什么:)
You just need to add the xmlns to your Window, and use that to reference your ValidationRule.
In WPF, the object is perfectly fine to be used from the same assembly.
Since your rule isn't defined in the standard XAML namespace, you have to create a mapping to your clr namespace like so:
And then you would use it like so:
Edit
I added a Path statement to the Binding. You have to tell the Binding what to bind to :)
Visual Studio 和 Expression Blend 中存在导致此问题的错误。 您需要做的是确保验证规则位于您可以引用的单独项目/程序集中。 这应该可以解决问题。
但是,您必须重新添加命名空间才能使其正常工作。
There is a bug in Visual Studio and Expression Blend that causes this problem. What you need to do is make sure that the Validation rule is in a separately project/assembly that you can reference. This should resolve the problem.
However, you will have to add back the namespace in order for it to work.
你的代码后面没有这一行
You do not have this line in ur code behind
我看到您在文本框上的绑定设置为“文本”路径 - 这是该文本框的数据上下文上的字段吗? 文本框实际上得到了一个值吗? 另外,如果您在验证方法中放置断点,是否会被解雇?
您可能想查找如何记录绑定失败并查看这些失败。
i see your binding on the TextBox is set to a path of 'Text' - is that a field on whatever the datacontext of this textbox is? is the textbox actually getting a value put into it? also, if you put a breakpoint in your validation method, is that ever getting fired?
you may want to lookup how to log failures in binding and review those as well..