WPF DependencyProperty验证绑定到对象属性 [英] WPF DependencyProperty Validation Binding to the object property

查看:190
本文介绍了WPF DependencyProperty验证绑定到对象属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试为给定控件创建验证规则(在这种情况下,它是TextBox).

I am trying to create a validation rule for a given control (in this scenario, it is the TextBox).

尽管采取了适当的步骤,但我无法获得对对象属性的成功绑定:利用ValidationRule和DepedencyProperty.

I am not able to obtain a successful Binding to the property of an object, although appropriate steps were taken: ValidationRule and DepedencyProperty are taken advantage of.

请在下面找到代码.附带说明一下,除非我在XAML中明确设置了值(根据"Is Ranged"参数,没有绑定),否则自定义Validation类中的是否需要"始终为False.

Kindly find code below. A side note is that "Is Required" in the custom Validation class is always False, unless I explicitly set the value in the XAML (no Binding, as per "Is Ranged" parameter).

任何提示和建议都值得赞赏.

Any tips and suggestions are appreciated.

预先感谢您:)

XAML代码:

<TextBox Style="{StaticResource ValidationError}" LostFocus="ForceValidationCheck"
         Visibility="{Binding Type, Converter={StaticResource Visibility}, ConverterParameter='Number'}"
         IsEnabled="{Binding RelativeSource={x:Static RelativeSource.Self}, Converter={StaticResource IsEnabled}}">
    <TextBox.Text>
        <Binding Path="Value">
            <Binding.ValidationRules>
                <validation:NumericValidation>
                    <validation:NumericValidation.Dependency>
                        <validation:NumericDependency IsRequired="{Binding Path=IsRequired}" IsRanged="True" Min="5"/>
                    </validation:NumericValidation.Dependency>
                </validation:NumericValidation>
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

验证类别:

public NumericDependency Dependency { get; set; }

public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
    isRequired = Dependency.IsRequired;
}

验证依赖类:

public static readonly DependencyProperty IsRequiredProperty =
        DependencyProperty.Register("IsRequired", typeof(bool), typeof(NumericDependency), new UIPropertyMetadata(default(bool)));

public bool IsRequired
{
    get
    {
        return (bool) GetValue(IsRequiredProperty);
    }
    set
    {
        SetValue(IsRequiredProperty, value);
    }
}

推荐答案

您可以使用代理.它将允许您将属性绑定到ValidationRule.

You can use a proxy. It will allow you to bind a Property to your ValidationRule.

代理示例

以下代码示例可能会为您提供帮助:

Here is a code sample that may help you :

<Utils:Proxy In="{Binding IsRequired, Mode=OneWay,UpdateSourceTrigger=PropertyChanged}" Out="{Binding ElementName=numericValidationRule, Path=IsRequired}" />
<TextBox>
    <TextBox.Text>
        <Binding Path="Value">
            <Binding.ValidationRules>
                <NumericValidation x:Name="numericValidationRule" />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

这篇关于WPF DependencyProperty验证绑定到对象属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆