WPF 验证文本框,绑定应该是什么样子?

发布于 2024-11-08 23:05:23 字数 2108 浏览 0 评论 0原文

我正在尝试验证 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2024-11-15 23:05:23

您必须将其绑定到源和路径,例如以下简单对象:

public  class Class1
{
    public string Name { get; set; }
}

可以通过将其添加到窗口的资源集合来创建该对象。
源绑定到对象及其属性 [Name] 的路径,在本例中为“0123456789”。

<Window.Resources>
    <validators:Class1 x:Key="class1" Name="0123456789" />
</Window.Resources>

最后,您必须通过 Source={StaticResource class1} 将其源绑定到此资源。

<Grid>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="118,60,0,0" Name="CreateUserCPRTextbox" VerticalAlignment="Top" Width="120" >
        <TextBox.Text >
            <Binding Source="{StaticResource class1}" 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>
</Grid>

接下来,您可以使用经过验证的文本框播放一些内容。

You have to bind it to a source and path, for example the following simple object:

public  class Class1
{
    public string Name { get; set; }
}

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

<Window.Resources>
    <validators:Class1 x:Key="class1" Name="0123456789" />
</Window.Resources>

Lastly, you have to bind its source to this resource by Source={StaticResource class1}

<Grid>
    <TextBox Height="23" HorizontalAlignment="Left" Margin="118,60,0,0" Name="CreateUserCPRTextbox" VerticalAlignment="Top" Width="120" >
        <TextBox.Text >
            <Binding Source="{StaticResource class1}" 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>
</Grid>

Next, you can play some with the validated textbox.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文