This is a migrated thread and some comments may be shown as answers.

Parameter count mismatch

2 Answers 29 Views
DataForm
This is a migrated thread and some comments may be shown as answers.
Ramon
Top achievements
Rank 1
Ramon asked on 21 Nov 2013, 11:22 AM
Hi

The dataform works well when AutoGenerateFields is true, but once i turn it off and supply it with 3 different DataTemplates it throws the Exception "Parameter count mismatch", the Object implements  IDataErrorInfo, IEditableObject and the problem seems to be the
IDataErrorInfo, any ideas
thanks

   at System.Reflection.RuntimeMethodInfo.InvokeArgumentsCheck(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimeMethodInfo.Invoke(Object obj, BindingFlags invokeAttr, Binder binder, Object[] parameters, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, BindingFlags invokeAttr, Binder binder, Object[] index, CultureInfo culture)
   at System.Reflection.RuntimePropertyInfo.GetValue(Object obj, Object[] index)
   at Telerik.Windows.Data.ItemPropertyInfoExtensions.GetValue(ItemPropertyInfo itemProperty, Object item) in c:\TB\105\WPF_Scrum\Release_WPF\Sources\Development\Core\Data\ItemProperties\ItemPropertyInfoExtensions.cs:line 105




2 Answers, 1 is accepted

Sort by
0
Ramon
Top achievements
Rank 1
answered on 21 Nov 2013, 11:29 AM
Here is the base Entity i use for the model ViewModelBase is Telerik.Windows.Controls, It seems that the DataForm cannot accept an object with inheritance and with interface  IDataErrorInfo the problem is with the Indexer "this[string propertyName]"
public abstract class BaseEntity : ViewModelBase, IEditableObject, IDataErrorInfo
   {
       #region Declarations
       [field: NonSerialized]
       ObservableCollection<BrokenRule> _brokenRules;
 
       [field: NonSerialized]
       Hashtable _props;
 
       [field: NonSerialized]
       bool _isInEditMode;
       #endregion
 
       public bool IsInEditMode
       {
           get { return _isInEditMode; }
           set
           {
               if (_isInEditMode != value)
               {
                   _isInEditMode = value;
                   OnPropertyChanged("IsInEditMode");
               }
           }
       }
       public void BeginEdit()
       {
           IsInEditMode = true;
           if (null != _props) return;
 
           var properties = (GetType()).GetProperties(BindingFlags.Public | BindingFlags.Instance);
 
           _props = new Hashtable(properties.Length - 1);
 
           foreach (var property in properties)
           {
               if (null != property.GetSetMethod())
               {
                   var value = property.GetValue(this, null);
                   _props.Add(property.Name, value);
               }
           }
       }
       public void CancelEdit()
       {
           if (null == _props) return;
 
           var properties = (GetType()).GetProperties(BindingFlags.Public | BindingFlags.Instance);
           foreach (var property in properties)
           {
               if (null != property.GetSetMethod())
               {
                   var value = _props[property.Name];
                   property.SetValue(this, value, null);
               }
           }
           _props = null;
           IsInEditMode = false;
       }
       public void EndEdit()
       {
           _props = null;
           IsInEditMode = false;
       }
 
       protected bool SetPropertyValue<T>(String propertyName, ref T currentValue, T newValue)
       {
           if (currentValue == null)
           {
               if (newValue == null)
                   return false;
           }
           else if (newValue != null && currentValue.Equals(newValue))
           {
               return false;
           }
 
           if (IsInEditMode)
           {
               currentValue = newValue;
               checkRulesForProperty(propertyName, newValue);
               OnPropertyChanged(propertyName);
               return true;
           }
           currentValue = newValue;
           return true;
       }
 
       private void checkRulesForProperty(String propertyName, Object value)
       {
           var foundBrokenRule = false;
           var hadBrokenRules = false;
 
           foreach (var item in (from r in BrokenRules where r.PropertyName == propertyName select r).ToList())
           {
               BrokenRules.Remove(item);
               hadBrokenRules = true;
           }
 
           var vrs = new List<ValidationResult>();
           if (!Validator.TryValidateProperty(value, new ValidationContext(this, null, null) { MemberName = propertyName }, vrs))
           {
               foundBrokenRule = true;
               foreach (var item in vrs)
               {
                   BrokenRules.Add(new BrokenRule(propertyName, item.ErrorMessage));
               }
           }
 
           if (foundBrokenRule || hadBrokenRules)
           {
               OnPropertyChanged("Error");
               OnPropertyChanged("BrokenValidationRulesCount");
           }
       }
 
       public Boolean IsValid()
       {
           return BrokenValidationRulesCount == 0;
       }
       public ObservableCollection<BrokenRule> BrokenRules
       {
           get { return _brokenRules ?? (_brokenRules = new ObservableCollection<BrokenRule>()); }
           protected set
           {
               _brokenRules = value;
               OnPropertyChanged("BrokenRules");
           }
       }
       public int BrokenValidationRulesCount
       {
           get { return BrokenRules.Count; }
       }
 
       public string Error
       {
           get
           {
               if (BrokenRules.Count > 0)
                   return String.Join(Environment.NewLine, BrokenRules
                       .Select(r => r.ErrorMessage).ToArray());
                
 
               return null;
           }
       }
       public string this[String propertyName]
       {
           get
           {
               if (BrokenRules.Count(r => r.PropertyName == propertyName) > 0)
               {
                   var ret = String.Join(Environment.NewLine, BrokenRules
                       .Where(r => r.PropertyName == propertyName).Select(r => r.ErrorMessage).ToArray());
                   return ret;
               }
 
               return String.Empty;
           }
       }
0
Ramon
Top achievements
Rank 1
answered on 21 Nov 2013, 12:39 PM
Ok found a solution for now in case anyone else tries to use IDataErrorInfo in a base class  you will need to use instead 
string IDataErrorInfo.Error
        {
            get
            {
                if (BrokenRules.Count > 0)
                {
                    return String.Join(Environment.NewLine, BrokenRules.Select(r => r.ErrorMessage).ToArray());
                }
 
                return String.Empty;
            }
        }
        string IDataErrorInfo.this[string columnName]
        {
            get
            {
                if (BrokenRules.Count(r => r.PropertyName == columnName) > 0)
                {
                    var ret = String.Join(Environment.NewLine, BrokenRules
                        .Where(r => r.PropertyName == columnName).Select(r => r.ErrorMessage).ToArray());
                    return ret;
                }
 
                return String.Empty;
            }
        }
Tags
DataForm
Asked by
Ramon
Top achievements
Rank 1
Answers by
Ramon
Top achievements
Rank 1
Share this question
or