using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Controls; using System.Windows; namespace HeuristicLab.OKB.Cockpit { public class StringParseValidationRule : ValidationRule { private Type type; public Type Type { get { return type; } set { if (type == value) return; if (type == null) return; switch (Type.GetTypeCode(value)) { // TODO parse nullables case TypeCode.Int32: break; case TypeCode.Double: break; case TypeCode.String: break; case TypeCode.DateTime: break; default: if (value.GetType() == typeof(Guid)) break; //throw new ArgumentException(String.Format( //"Parsing validation of type {0} is not supported", value)); break; } type = value; } } public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) { if (type == null) return new ValidationResult(true, null); try { string s = (string)value; switch (Type.GetTypeCode(type)) { case TypeCode.Int32: int.Parse(s); break; case TypeCode.Double: double.Parse(s); break; case TypeCode.String: break; case TypeCode.DateTime: DateTime.Parse(s); break; default: if (value.GetType() == typeof(Guid)) new Guid(s); else throw new InvalidOperationException(); break; } return new ValidationResult(true, null); } catch { return new ValidationResult(false, String.Format("'{0}' is not a valid '{1}'", value, Type.Name)); } } } }