2012-02-09 87 views
0

我是WPF的新手,对绑定集合的验证存在问题。如何实现绑定集合的绑定验证

我有DataGrid绑定到多个对象组合在一起的集合。

数据网格具有两列:

  • 拉布勒一次性界,显示名称当文本框引发LostFocus

数据网格中的每一行为界

  • 文本框双向一定到值来自基类的一个对象。每个派生类都有自己的验证方法,我正在尝试基于派生类的实例为TextBox执行适当的验证方法。

    我可以绑定基于ExceptionValidationRule的验证,但性能不是很好,应用程序闪烁,因为它有很多例外。我也知道我可以这样做,抛出TextBox LostFocus的事件并获取源对象,但我试图对有界对象使用WPF验证(如果可能的话)。

    我试图继承有效性规则的基类,并通过它,但它与基类的函数输入,而不是与派生界项目 我试图创建实现的有效性规则和DependencyObject的单独的类和我试图传递给它像这里解释的源对象:http://dedjo.blogspot.com/2007/05/fully-binded-validation-by-using.html

    但我仍然无法让它工作。为了简单起见,我创建了一个具有TestString属性的DependencyObject,并试图将其绑定到显示名称(Label的有界路径)。但是TestString的集合永远不会被调用。

    我该怎么做?我怎样才能调用适当的验证方法?我能否以某种方式将有界对象传递给Valuator?

    感谢您的帮助, 沙立

    我使用的代码:

    实现类的DependencyObject:

    class ManifestPropertyDependency : DependencyObject 
    { 
    
        private ManifestProperty _manifestPropertyInstance; 
        public ManifestProperty ManifestPropertyInstance 
        { 
         get { return (ManifestProperty)GetValue(ManifestPropertyInstanceProp); } 
         set 
         { 
          SetValue(ManifestPropertyInstanceProp, value); 
         } 
        } 
    
        private string testString; 
        public string TestString { 
         get { return (string)GetValue(TestStringProp); } 
         set { SetValue(TestStringProp, value); } 
        } 
        public ManifestPropertyDependency() 
        { 
         testString = ""; 
         _manifestPropertyInstance = new ManifestProperty(); 
        } 
        public static readonly DependencyProperty ManifestPropertyInstanceProp = 
         DependencyProperty.Register("ManifestPropertyInstance", typeof(ManifestProperty), 
         typeof(ManifestPropertyDependency), new UIPropertyMetadata(new ManifestProperty())); 
    
        public static readonly DependencyProperty TestStringProp = 
         DependencyProperty.Register("TestString", typeof(string), 
         typeof(ManifestPropertyDependency), new UIPropertyMetadata("")); 
    } 
    

    实现类有效性规则的:

    class ManifestPropertyValidator : ValidationRule 
    { 
        private ManifestProperty _manifest; 
        private ManifestPropertyDependency _manifestPropertyDependency; 
        public string Stam { get { return _manifestPropertyDependency.TestString; } set { _manifestPropertyDependency.TestString = value; } } 
        public ManifestPropertyDependency ManifestPropertyDependency 
        { 
         get { return _manifestPropertyDependency; } 
    
    
         set 
         { 
          _manifestPropertyDependency = value; 
          Stam = value.TestString; 
          _manifest = value.ManifestPropertyInstance; 
         } 
        } 
    
    
        public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) 
        { 
         try 
         { 
          string errorMessage = ""; 
          if (ManifestPropertyDependency.ManifestPropertyInstance.ValidateString((string)value, ref errorMessage)) 
          { 
           return new ValidationResult(true, null); 
          } 
          return new ValidationResult(false, errorMessage); 
         } 
         catch (Exception e) 
         { 
          return new ValidationResult(false, "Illegal characters or " + e.Message); 
         } 
        } 
    } 
    

    XAML中的验证:

    <TextBox Validation.ErrorTemplate="{StaticResource validationTemplate}" 
               Style="{StaticResource textStyleTextBox}"> 
        <TextBox.Text> 
         <Binding Path="Value" Mode="TwoWay" 
           UpdateSourceTrigger="LostFocus"> 
          <Binding.ValidationRules> 
           <Classes:ManifestPropertyValidator> 
            <Classes:ManifestPropertyValidator.ManifestPropertyDependency> 
             <Classes:ManifestPropertyDependency TestString="{Binding Path=DisplayName}"/> 
            </Classes:ManifestPropertyValidator.ManifestPropertyDependency> 
           </Classes:ManifestPropertyValidator> 
          </Binding.ValidationRules> 
         </Binding> 
        </TextBox.Text> 
    </TextBox> 
    
  • +0

    在类ManifestPropertyDependency中,您为依赖项属性定义了支持字段“_manifestPropertyInstance”和“testString”。这是错误的! DP不需要支持字段。他们的getter和setter调用'DependencyObject.GetValue'和'DependencyObject.GetValue'来代替(就像你做的那样)。像在构造函数中那样,将值分配给未使用的后备字段是毫无意义的。改为设置属性。而且,当您在XAML中使用这些DP时,WPF **不会**调用getter或setter,而是直接访问底层DP。 – Clemens 2012-02-09 09:47:49

    +0

    我知道支持字段不是nessesary,我只是为了调试。我试图了解DependencyObject中发生了什么。应该从ManifestPropertyValidator调用DependencyObject的获取和设置,并试图查看这些字段中的内容。 – user1197547 2012-02-09 12:35:03

    回答

    0

    老实说,我想不出underastand你的代码,但是看着你的要求,我觉得这简直是IDataErrorInfo接口的候选人。

    为什么你不使用它呢?

    此外,您还谈到了验证缓慢。你能详细说明一下吗?你的模板很慢吗?你的验证逻辑很慢吗?或者你认为验证通知本身很慢?

    +0

    我试图看看IDataErrorInfo,但我不明白如何使用它。谁应该实现接口?我应该用一个执行IDataErrorInfo的类声明多态对象吗?关于Slowness,应用程序只是闪烁,而不会在一段时间后响应。我认为这是由于异常验证,因为当我删除它的应用程序工作正常。 – user1197547 2012-02-09 12:29:28

    +0

    看到这个实现... http://www.codeproject.com/Articles/15239/Validation-in-Windows-Presentation-Foundation – 2012-02-09 12:49:33

    +0

    它的工作。谢谢 – user1197547 2012-02-09 15:19:59