在WPF中使用依赖属性和样式触发器中的验证

时间:2021-12-11 15:57:04

I am trying to use Validation in WPF. I created a NotNullOrEmptyValidationRule as shown below:

我试图在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); 
        }
    }

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.

现在,我需要在我的应用程序中使用它。在我的App.xaml文件中,我为TextBox声明了Style。这是宣言。

 <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上使用它,所以我使用以下代码:

  <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.

Tag NotNullOrEmptyValidationRule上出现错误。 XAML语法检查程序无法解析NotNullOrEmptyValidationRule。我甚至尝试过放置命名空间但它似乎不起作用。

4 个解决方案

#1


1  

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?

我看到你在TextBox上的绑定被设置为'Text'的路径 - 这个文本框的datacontext上的字段是什么?文本框实际上是否获得了一个值?另外,如果你在验证方法中放置一个断点,那会被解雇吗?

you may want to lookup how to log failures in binding and review those as well..

您可能想要查找如何记录绑定中的失败并查看这些失败。

#2


5  

You just need to add the xmlns to your Window, and use that to reference your ValidationRule.

您只需将xmlns添加到Window,并使用它来引用您的ValidationRule。

In WPF, the object is perfectly fine to be used from the same assembly.

在WPF中,对象完全可以在同一个程序集中使用。

Since your rule isn't defined in the standard XAML namespace, you have to create a mapping to your clr namespace like so:

由于您的规则未在标准XAML命名空间中定义,因此您必须创建到clr命名空间的映射,如下所示:

<Window ...
    xmlns:local="clr-namespace:MyNamespaceName">

And then you would use it like so:

然后你会像这样使用它:

<Binding Path=".">
    <Binding.ValidationRules>
        <local:NotNullOrEmptyValidationRule />
    </Binding.ValidationRules>
</Binding>

Edit I added a Path statement to the Binding. You have to tell the Binding what to bind to :)

编辑我在Binding中添加了一个Path语句。你必须告诉绑定要绑定的内容:)

#3


1  

You do not have this line in ur code behind

你的代码背后没有这一行

Public Sub New()

    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    Me.**NameOfTextBox**.DataContext = Me
End Sub

#4


0  

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.

Visual Studio和Expression Blend中存在导致此问题的错误。您需要做的是确保验证规则位于您可以引用的单独项目/程序集中。这应该可以解决问题。

However, you will have to add back the namespace in order for it to work.

但是,您必须添加命名空间才能使其正常工作。

#1


1  

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?

我看到你在TextBox上的绑定被设置为'Text'的路径 - 这个文本框的datacontext上的字段是什么?文本框实际上是否获得了一个值?另外,如果你在验证方法中放置一个断点,那会被解雇吗?

you may want to lookup how to log failures in binding and review those as well..

您可能想要查找如何记录绑定中的失败并查看这些失败。

#2


5  

You just need to add the xmlns to your Window, and use that to reference your ValidationRule.

您只需将xmlns添加到Window,并使用它来引用您的ValidationRule。

In WPF, the object is perfectly fine to be used from the same assembly.

在WPF中,对象完全可以在同一个程序集中使用。

Since your rule isn't defined in the standard XAML namespace, you have to create a mapping to your clr namespace like so:

由于您的规则未在标准XAML命名空间中定义,因此您必须创建到clr命名空间的映射,如下所示:

<Window ...
    xmlns:local="clr-namespace:MyNamespaceName">

And then you would use it like so:

然后你会像这样使用它:

<Binding Path=".">
    <Binding.ValidationRules>
        <local:NotNullOrEmptyValidationRule />
    </Binding.ValidationRules>
</Binding>

Edit I added a Path statement to the Binding. You have to tell the Binding what to bind to :)

编辑我在Binding中添加了一个Path语句。你必须告诉绑定要绑定的内容:)

#3


1  

You do not have this line in ur code behind

你的代码背后没有这一行

Public Sub New()

    ' This call is required by the Windows Form Designer.
    InitializeComponent()

    Me.**NameOfTextBox**.DataContext = Me
End Sub

#4


0  

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.

Visual Studio和Expression Blend中存在导致此问题的错误。您需要做的是确保验证规则位于您可以引用的单独项目/程序集中。这应该可以解决问题。

However, you will have to add back the namespace in order for it to work.

但是,您必须添加命名空间才能使其正常工作。