2010-11-28 69 views
1

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

+1

你能否提供一些关于你正在努力实现的内容(输入,预期输出等)的更多信息? – Moonshield 2010-11-28 12:35:50

回答

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

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

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> 

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

+0

干得好,但如果你尝试绑定它,你会得到一个'找不到管理FrameworkElement或FrameworkContentElement的目标元素绑定错误。 Thomas Levesque对此有一个答案,请参阅http://www.thomaslevesque.com/2011/03/21/wpf-how-to-bind-to-data-when-the-datacontext-is-not-inherited/ – 2013-12-19 23:53:19