Attached or dependecy Property for ValidationRule WPF

风格不统一 提交于 2019-12-11 12:14:08

问题


I want to bind the attached property or dependency property in xaml for the ValidationRule in xaml and then based on the value of the attached property or dependency property I want to make sum decision in the Validation rule. I can't find any solution how can I Pass bindable value to the Validation Rule.


回答1:


I supply you a sample code to help you. I have defined a ValidationRule to validate a texbox user input. The type of validation is performed according value of one enum parameter. Type of validation available are: user input cannot be empty, user input must be numeric, user input must be an IP address. A second parameter allows to specificy warning message displayed. As you know a variable for binding purposes should be a DependendyProperty, so here you find class with paramaters declaration.

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

Then here is the validationrule class:

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

And finally here is the XAML code:

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

You can see that parameter Message is binded to a resource, but you can classically bind it too.



来源:https://stackoverflow.com/questions/4296507/attached-or-dependecy-property-for-validationrule-wpf

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!