[4311] | 1 | using System;
|
---|
| 2 | using System.Collections.Generic;
|
---|
| 3 | using System.Linq;
|
---|
| 4 | using System.Text;
|
---|
| 5 | using System.Windows.Controls;
|
---|
| 6 | using System.Windows;
|
---|
| 7 |
|
---|
| 8 | namespace HeuristicLab.OKB.Cockpit {
|
---|
| 9 | public class StringParseValidationRule : ValidationRule {
|
---|
| 10 |
|
---|
| 11 | private Type type;
|
---|
| 12 | public Type Type {
|
---|
| 13 | get {
|
---|
| 14 | return type;
|
---|
| 15 | }
|
---|
| 16 | set {
|
---|
| 17 | if (type == value)
|
---|
| 18 | return;
|
---|
| 19 | if (type == null)
|
---|
| 20 | return;
|
---|
| 21 | switch (Type.GetTypeCode(value)) {
|
---|
| 22 | // TODO parse nullables
|
---|
| 23 | case TypeCode.Int32: break;
|
---|
| 24 | case TypeCode.Double: break;
|
---|
| 25 | case TypeCode.String: break;
|
---|
| 26 | case TypeCode.DateTime: break;
|
---|
| 27 | default:
|
---|
| 28 | if (value.GetType() == typeof(Guid))
|
---|
| 29 | break;
|
---|
| 30 | //throw new ArgumentException(String.Format(
|
---|
| 31 | //"Parsing validation of type {0} is not supported", value));
|
---|
| 32 | break;
|
---|
| 33 | }
|
---|
| 34 | type = value;
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
| 38 | public override ValidationResult Validate(object value, System.Globalization.CultureInfo cultureInfo) {
|
---|
| 39 | if (type == null)
|
---|
| 40 | return new ValidationResult(true, null);
|
---|
| 41 | try {
|
---|
| 42 | string s = (string)value;
|
---|
| 43 | switch (Type.GetTypeCode(type)) {
|
---|
| 44 | case TypeCode.Int32: int.Parse(s); break;
|
---|
| 45 | case TypeCode.Double: double.Parse(s); break;
|
---|
| 46 | case TypeCode.String: break;
|
---|
| 47 | case TypeCode.DateTime: DateTime.Parse(s); break;
|
---|
| 48 | default:
|
---|
| 49 | if (value.GetType() == typeof(Guid))
|
---|
| 50 | new Guid(s);
|
---|
| 51 | else
|
---|
| 52 | throw new InvalidOperationException();
|
---|
| 53 | break;
|
---|
| 54 | }
|
---|
| 55 | return new ValidationResult(true, null);
|
---|
| 56 | } catch {
|
---|
| 57 | return new ValidationResult(false, String.Format("'{0}' is not a valid '{1}'", value, Type.Name));
|
---|
| 58 | }
|
---|
| 59 | }
|
---|
| 60 | }
|
---|
| 61 | }
|
---|