ã¯ããã«
ãä»åã®è¨äºã¯ãååã¾ã§ã«å ãã¦ãå¿ç¨çãªå 容ãç´¹ä»ãã¾ãããã¼ã¿ã®å ¥åæ¤è¨¼ããã¼ã¿ã®è¿½å ãã¹ãã¢ãããã·ã¼ã¸ã£ã®å®è¡ãªã©ãTipsçãªãããã¯ãé çªã«ç´¹ä»ãããã¨æãã¾ãã
ãã¾ãããã®é£è¼ã¯ãµã³ãã«ã·ã¹ãã ããã¼ã¹ã«èª¬æãè¡ãã¾ãã以ä¸ã®ãµã¤ããããµã³ãã«ã³ã¼ããå®è¡ãã¡ã¤ã«ãå ¥æãã¦ã確èªããªããèªã¿é²ãã¦ããã ããã¨ãããä¸å±¤ç解ãæ·±ã¾ãã¨æãã¾ãã
ãµã³ãã«ã¢ããªã±ã¼ã·ã§ã³ã®å ¬éãµã¤ã
Silverlightã¢ããªã±ã¼ã·ã§ã³ã®å ¥åæ¤è¨¼æ©è½
ãSilverlight 3ã§å ¥åæ¤è¨¼æ©è½ãèããå ´åãååç´¹ä»ãããã¤ã³ãã£ã³ã°ã®æ©è½ãçããã¦ãã¨ã³ãã£ãã£ã¯ã©ã¹ã®ä¸ã§è¡ãææ³ãå ¸åçãªå½¢ã«ãªãã§ãããããã¤ã³ãã£ã³ã°ãããåããããã£ã®Setterã§ãã§ãã¯å¦çãè¡ããä¾å¤ãã¹ãã¼ããããã«å®è£ ãã¾ãããã®ã¨ã該å½ã³ã³ããã¼ã«ã®ValidatesOnExceptionsããããã£ãTrueã«ããã¨ãTwoWayãã¤ã³ãã£ã³ã°ã®ã¿ã¼ã²ããï¼ã¤ã¾ãã³ã³ããã¼ã«å´ï¼ããã½ã¼ã¹ãªãã¸ã§ã¯ããå¤æ´ããæã«çºçããä¾å¤ãããã¤ã³ãã£ã³ã°ã¨ã³ã¸ã³ããã£ãããã¦ããã¾ãã
<TextBox Grid.Row="4" Grid.Column="1" Text="{Binding Quantity, Mode=TwoWay, ValidatesOnExceptions=True}" />
ãã¨ã³ãã£ãã£ã¯ã©ã¹ã確èªãã¦ããã¤ã³ã対象ã§ããQuantityãè¦ã¦ã¿ã¾ããããSetterã§Exceptionãçºçããã¦ãããã¨ããããã¾ãã
private int quantity = 1; public int Quantity { get { return this.quantity; } set { if (this.quantity != value) { if (1 > value || value > 99) { throw new ArgumentException("æ°é㯠1 - 99 ã®éã§å ¥åãã¦ãã ããã"); } this.quantity = value; PropertyChanged(this, new PropertyChangedEventArgs("Quantity")); } } }
ããã®å®è£ ã«ãããå®è¡æã«ã¯æ¬¡ã®ããã«å ¥åæ¤è¨¼ãè¡ããã¨ãã§ããããã«ãªãã¾ãã
ãã¾ããSilverlight 4ããã¯ãããã¦Setterã§Exceptionãçºçããããã¨ãªããå ¥åæ¤è¨¼æ©è½ãå®è£ ã§ãã以ä¸2ã¤ã®ã¤ã³ã¿ã¼ãã§ã¼ã¹ãæä¾ãã¾ãã
- IDataErrorInfo
- INotifyDataErrorInfo
ãããããã£ã¬ãã«ã§ã®å¦¥å½æ§æ¤æ»ã®ã¨ã©ã¼éç¥
ãã¨ã³ãã£ãã£ã¬ãã«ã§ã®å¦¥å½æ§æ¤æ»ã®ã¨ã©ã¼éç¥
ãç¹ã«INotifyDataErrorInfoã¯Silverlight 4ç¹æãªå®è£ ã«ãªããæè»ãªä½¿ç¨ãè¦è¾¼ãããããç°¡åã«ç´¹ä»ãã¦ããã¾ãã
ãä¾ãã°ãå§ã¨åã®2ã¤ã®å ¥åæ ããããã©ã¡ããä¸æ¹ã¯å ¥åããã¦ããå¿ è¦ãããã¨ããã«ã¼ã«ãä½æãããå ´åãèãã¾ãããã®å ´åãä¸è¨ã®ã¨ã³ãã£ãã£ã¯ã©ã¹ã®ä¸é¨ã®ããã«å®è£ ãè¡ãã¾ãã
private string lastname; public string LastName { get { return lastname; } set { lastname = value; this.OnPropertyChanged("LastName"); this.OnErrorsChanged("LastName"); this.OnErrorsChanged("FirstName"); } } private string firstname; public string FirstName { get { return firstname; } set { firstname = value; this.OnPropertyChanged("FirstName"); this.OnErrorsChanged("FirstName"); this.OnErrorsChanged("LastName"); } } void OnErrorsChanged(string propertyName) { if (ErrorsChanged != null) ErrorsChanged(this, new DataErrorsChangedEventArgs(propertyName)); }
ãLastNameï¼å§ï¼FirstNameï¼åï¼ãSetterã§2åOnErrorsChangedãå®è¡ãã¦ãã¾ãããã®çµæã以ä¸ã®GetErrorsãå®è¡ããã¾ãã
public System.Collections.IEnumerable GetErrors(string propertyName) { switch (propertyName) { case "LastName": case "FirstName": if (string.IsNullOrEmpty(lastname) && string.IsNullOrEmpty(firstname)) return new[] { "å§åã®ã©ã¡ããã¯å ¥åãã¦ãã ãã"}; break; } return null; }
ããã®çµæãä¸å³ã®ããã«ã2ã¤ã®ã³ã³ããã¼ã«ã«å¯¾ãã¦åæã«è¦åã表示ï¼æ¶å»ï¼ã§ããããã«ãªãã¾ãã
ããã®ããã«ã¨ã³ãã£ãã£å ¨ä½ããããã¯è¤æ°ã®ã³ã³ããã¼ã«ã«ã¾ãããå ¥åæ¤è¨¼ãç°¡åã«å®è£ ã§ããã®ãSilverlight 4ã®ç¹å¾´ã®1ã¤ã§ãã