连接或有效性规则WPF dependecy物业(Attached or dependecy Prop

2019-09-29 04:12发布

我想根据附加属性或依赖属性我想在验证规则和决策的价值的附加属性或XAML在XAML依赖属性的有效性规则,然后绑定。 我找不到任何解决方案如何传递绑定值的验证规则。

Answer 1:

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

    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); }
    }

然后这里是有效性规则类:

    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>

你可以看到,参数消息被绑定到的资源,但你可以绑定经典太。



文章来源: Attached or dependecy Property for ValidationRule WPF