■ FrameworkElement 클래스의 BindingValidationError 이벤트를 사용하는 방법을 보여준다.
▶ 예제 코드 (XAML)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
<StackPanel xmlns:local="clr-namespace:SilverlightApplication1" BindingValidationError="stackPanel_BindingValidationError"> <StackPanel.Resources> <local:Bill x:Name="BillKey" /> </StackPanel.Resources> <TextBox x:Name="textBox" Margin="10" Width="50"> <TextBox.Text> <Binding Source="{StaticResource BillKey}" Path="Amount" Mode="TwoWay" ValidatesOnExceptions="true" NotifyOnValidationError="true" /> </TextBox.Text> </TextBox> <Button Width="150" Height="50" Content="Click To Update Source" /> </StackPanel> |
▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
using System; using System.Windows.Controls; using System.Windows.Media; /// <summary> /// 청구서 /// </summary> public class Bill { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 금액 /// </summary> private double amount; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 금액 - Amount /// <summary> /// 금액 /// </summary> public double Amount { get { return this.amount; } set { if(value < 0d) { throw new Exception("Amount must be greater than zero."); } this.amount = value; } } #endregion } ... #region 스택 패널 바인딩 무결성 에러 발생시 처리하기 - stackPanel_BindingValidationError(sender, e) /// <summary> /// 스택 패널 바인딩 무결성 에러 발생시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void stackPanel_BindingValidationError(object sender, ValidationErrorEventArgs e) { if(e.Action == ValidationErrorEventAction.Added) { this.textBox.Background = new SolidColorBrush(Colors.Red); } else if(e.Action == ValidationErrorEventAction.Removed) { this.textBox.Background = new SolidColorBrush(Colors.White); } } #endregion |