验证规则WPF的附加或依赖属性

时间:2010-11-28 11:27:20

标签: wpf dependency-properties attached-properties

我想在xaml中为xaml中的ValidationRule绑定附加属性或依赖项属性,然后根据我想在验证规则中进行求和决策的附加属性或依赖项属性的值。我找不到任何解决方案 如何将可绑定值传递给验证规则。

1 个答案:

答案 0 :(得分:2)

我为您提供示例代码以帮助您。我已经定义了ValidationRule来验证texbox用户输入。验证类型根据一个枚举参数的值执行。可用的验证类型是:用户输入不能为空,用户输入必须为数字,用户输入必须是IP地址。第二个参数允许显示特定的警告消息。如您所知,用于绑定目的的变量应该是DependendyProperty,所​​以在这里您可以找到带有参数声明的类。

    public class ValidationParams : DependencyObject
{
    // Dependency Properties
    public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message",
                                                                                          typeof(string),
                                                                                          typeof(ValidationParams),
                                                                                          new FrameworkPropertyMetadata(string.Empty));

    public static readonly DependencyProperty ValidationTypeProperty = DependencyProperty.Register("ValidationType",
                                                                                          typeof(FieldValidationRule.EnmValidationType),
                                                                                          typeof(ValidationParams),
                                                                                          new FrameworkPropertyMetadata(FieldValidationRule.EnmValidationType.FieldNotEmpty));

    // Properties
    [Category("Message")]
    public string Message
    {
        get { return (string)GetValue(MessageProperty); }
        set { SetValue(MessageProperty, value); }
    }

    [Category("ValidationType")]
    public FieldValidationRule.EnmValidationType ValidationType
    {
        get { return (FieldValidationRule.EnmValidationType)GetValue(ValidationTypeProperty); }
        set { SetValue(ValidationTypeProperty, value); }
    }

然后这是validationrule类:

    public class FieldValidationRule : ValidationRule
{
    public enum EnmValidationType
    {
        FieldNotEmpty,
        FieldNumeric,
        FieldIPAddress
    }

    // Local variables and objects
    private ValidationParams mParams = new ValidationParams();

    public ValidationParams Params
    {
        get { return mParams; }
        set { mParams = value; }
    }

    // Override
    public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo)
    {
        ValidationResult objResult = null;
        string sValue = value as string;
        objResult = new ValidationResult(true, null);
        switch (Params.ValidationType)
        {
            case EnmValidationType.FieldNotEmpty:
                if(string.IsNullOrEmpty(sValue) == true)
                    objResult = new ValidationResult(false, Params.Message);
                break;
            case EnmValidationType.FieldNumeric:
                int iValue = 0;
                if(int.TryParse(sValue, out iValue) == false)
                    objResult = new ValidationResult(false, Params.Message);
                break;
            case EnmValidationType.FieldIPAddress:
                IPAddress objValue = IPMatrix.CreateHostAddr();
                if(IPAddress.TryParse(sValue, out objValue) == false)
                    objResult = new ValidationResult(false, Params.Message);
                break;
        }
        return objResult;
    }
}

最后这是XAML代码:

                        <TextBox Style="{DynamicResource FieldValue}" Grid.Column="1" IsReadOnly="False">
                        <TextBox.Text>
                            <Binding Source="{StaticResource XmlItemChannel}" XPath="@Name" Mode="TwoWay" UpdateSourceTrigger="LostFocus">
                                <Binding.ValidationRules>
                                    <data:FieldValidationRule>
                                        <data:FieldValidationRule.Params>
                                            <data:ValidationParams Message="{DynamicResource ERR002}" ValidationType="FieldNotEmpty" />
                                        </data:FieldValidationRule.Params>
                                    </data:FieldValidationRule>
                                </Binding.ValidationRules>
                            </Binding>
                        </TextBox.Text>
                    </TextBox>

您可以看到参数Message绑定到资源,但您也可以通过它进行经典绑定。