【www.gdgbn.com--网页配色】

validation:inotifypropertychanged实例
validation大概分以下几种:

类型和长度:比如 d{1,3} 最多三位,必须数字
范围:比如a-za-z, 大于100小于200等
商业逻辑:特殊的,比如检查数据库教程某name唯一(需调用后台service),或者其他的business logic等

namespace system.componentmodel

  2: {

  3:     public interface inotifydataerrorinfo

  4:     {

  5:         bool haserrors { get; }

  6:         event eventhandler errorschanged;

  7:         ienumerable geterrors(string propertyname);

  8:     }

  9: }

没来

public abstract class myxxx: inotifypropertychanged, inotifydataerrorinfo

  2: {

  3:         private errorscontainer errorscontainer;

  4:

  5:         ///

  6:         /// initializes a new instance of the class.

  7:         ///

  8:         protected myxxx ()

  9:         {

 10:         }

 11:

 12:         ///

 13:         /// event raised when a property value changes.

 14:         ///

 15:         ///

 16:         public event propertychangedeventhandler propertychanged;

 17:

 18:         ///

 19:         /// event raised when the validation status changes.

 20:         ///

 21:         ///

 22:         public event eventhandler errorschanged;

 23:

 24:         ///

 25:         /// gets the error status.

 26:         ///

 27:         ///

 28:         public bool haserrors

 29:         {

 30:             get { return this.errorscontainer.haserrors; }

 31:         }

 32:

 33:         ///

 34:         /// gets the container for errors in the properties of the domain object.

 35:         ///

 36:         protected errorscontainer errorscontainer

 37:         {

 38:             get

 39:             {

 40:                 if (this.errorscontainer == null)

 41:                 {

 42:                     this.errorscontainer =

 43:                         new errorscontainer(pn => this.raiseerrorschanged(pn));

 44:                 }

 45:

 46:                 return this.errorscontainer;

 47:             }

 48:         }

 49:

 50:         ///

 51:         /// returns the errors for .

 52:         ///

 53:         /// the name of the property for which the errors are requested.

 54:         /// an enumerable with the errors.

 55:         ///

 56:         public ienumerable geterrors(string propertyname)

 57:         {

 58:             return this.errorscontainer.geterrors(propertyname);

 59:         }

 60:

 61:         ///

 62:         /// raises the event.

 63:         ///

 64:         /// the name of the changed property.

 65:         [suppressmessage("microsoft.design", "ca1030:useeventswhereappropriate", justification = "method supports event.")]

 66:         protected void raisepropertychanged(string propertyname)

 67:         {

 68:             this.onpropertychanged(new propertychangedeventargs(propertyname));

 69:         }

 70:

 71:         ///

 72:         /// raises the event.

 73:         ///

 74:         /// the argument for the event.

 75:         protected virtual void onpropertychanged(propertychangedeventargs e)

 76:         {

 77:             var handler = this.propertychanged;

 78:             if (handler != null)

 79:             {

 80:                 handler(this, e);

 81:             }

 82:         }

 83:

 84:         ///

 85:         /// validates as the value for the property named .

 86:         ///

 87:         /// the name of the property.

 88:         /// the value for the property.

 89:         protected void validateproperty(string propertyname, object value)

 90:         {

 91:             if (string.isnullorempty(propertyname))

 92:             {

 93:                 throw new argumentnullexception("propertyname");

 94:             }

 95:

 96:             this.validateproperty(new validationcontext(this, null, null) { membername = propertyname }, value);

 97:         }

 98:

 99:         ///

100:         /// validates as the value for the property specified by

101:         /// using data annotations validation attributes.

102:         ///

103:         /// the context for the validation.

104:         /// the value for the property.

105:         protected virtual void validateproperty(validationcontext validationcontext, object value)

106:         {

107:             if (validationcontext == null)

108:             {

109:                 throw new argumentnullexception("validationcontext");

110:             }

111:

112:             list validationresults = new list();

113:             validator.tryvalidateproperty(value, validationcontext, validationresults);

114:

115:             this.errorscontainer.seterrors(validationcontext.membername, validationresults);

116:         }

117:

118:         ///

119:         /// raises the event.

120:         ///

121:         /// the name of the property which changed its error status.

122:         [suppressmessage("microsoft.design", "ca1030:useeventswhereappropriate", justification = "method supports event.")]

123:         protected void raiseerrorschanged(string propertyname)

124:         {

125:             this.onerrorschanged(new dataerrorschangedeventargs(propertyname));

126:         }

127:

128:         ///

129:         /// raises the event.

130:         ///

131:         /// the argument for the event.

132:         protected virtual void onerrorschanged(dataerrorschangedeventargs e)

133:         {

134:             var handler = this.errorschanged;

135:             if (handler != null)

136:             {

137:                 handler(this, e);

138:             }

139:         }

140:     }

本文来源:http://www.gdgbn.com/wangyezhizuo/29435/