■ BindingGroup 엘리먼트를 사용해 바인딩을 검증하는 방법을 보여준다.
▶ BindingGroupValidationRule.cs
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 |
using System.Globalization; using System.Windows.Controls; using System.Windows.Data; namespace TestProject { /// <summary> /// 바인딩 그룹 검증 규칙 /// </summary> public class BindingGroupValidationRule : ValidationRule { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 검증하기 - Validate(value, cultureInfo) /// <summary> /// 검증하기 /// </summary> /// <param name="value">값</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>검증 결과</returns> public override ValidationResult Validate(object value, CultureInfo cultureInfo) { BindingGroup bindingGroup = value as BindingGroup; DataItem1 dataItem1 = null; DataItem2 dataItem2 = null; foreach(object item in bindingGroup.Items) { if(item is DataItem1) { dataItem1 = item as DataItem1; } if(item is DataItem2) { dataItem2 = item as DataItem2; } } if(dataItem1 == null || dataItem2 == null) { return new ValidationResult(false, "소스 객체에서 바인딩 그룹을 찾을 수 없습니다."); } string value1 = bindingGroup.GetValue(dataItem1, "Value") as string; string value2 = bindingGroup.GetValue(dataItem2, "Value") as string; if(value1 != value2) { return new ValidationResult(false, "두 문자열이 동일해야 합니다."); } return ValidationResult.ValidResult; } #endregion } } |
▶ MainWindow.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 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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="BindingGroup 엘리먼트 : 바인딩 검증하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:DataItem1 x:Key="DataItem1Key" /> <local:DataItem2 x:Key="DataItem2Key" /> </Window.Resources> <Grid Margin="10"> <StackPanel HorizontalAlignment="Center" VerticalAlignment="Center"> <StackPanel Name="stackPanel" HorizontalAlignment="Center" Margin="10" Orientation="Horizontal" Validation.ValidationAdornerSite="{Binding ElementName=messageLabel}"> <StackPanel.BindingGroup> <BindingGroup Name="bindingGroup"> <BindingGroup.ValidationRules> <local:BindingGroupValidationRule ValidatesOnTargetUpdated="True" /> </BindingGroup.ValidationRules> </BindingGroup> </StackPanel.BindingGroup> <TextBlock Margin="10" Text="첫번째 문자열" /> <TextBox Width="150" Height="25" VerticalContentAlignment="Center" Text="{Binding Source={StaticResource DataItem1Key}, Path=Value, BindingGroupName=bindingGroup}" /> <TextBlock Margin="10" Text="두번째 문자열" /> <TextBox Width="150" Height="25" VerticalContentAlignment="Center" Text="{Binding Source={StaticResource DataItem2Key}, Path=Value, BindingGroupName=bindingGroup, TargetNullValue='문자열을 입력해주시기 바랍니다.'}" /> </StackPanel> <Label Name="messageLabel" HorizontalAlignment="Center" Margin="10" Padding="5" Foreground="Red" Content="{Binding ElementName=stackPanel, Path=(Validation.Errors)[0].ErrorContent}" /> <Button Name="submitButton" HorizontalAlignment="Center" Margin="10" Width="100" Height="30" IsDefault="True"> 제출하기(_S) </Button> <StackPanel HorizontalAlignment="Left" Margin="10" Orientation="Horizontal"> <TextBlock Margin="10" FontWeight="Bold" Text="첫번째 문자열 :" /> <TextBlock Margin="10" Text="{Binding Source={StaticResource DataItem1Key}, Path=Value, TargetNullValue='--'}" /> </StackPanel> <StackPanel HorizontalAlignment="Left" Margin="10" Orientation="Horizontal"> <TextBlock Margin="10" FontWeight="Bold" Text="두번째 문자열 :" /> <TextBlock Margin="10" Text="{Binding Source={StaticResource DataItem2Key}, Path=Value, TargetNullValue='--'}" /> </StackPanel> </StackPanel> </Grid> </Window> |
▶ MainWindow.xaml.cs
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 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.submitButton.Click += submitButton_Click; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 제출하기 버튼 클릭시 처리하기 - submitButton_Click(sender, e) /// <summary> /// 제출하기 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void submitButton_Click(object sender, RoutedEventArgs e) { this.stackPanel.BindingGroup.UpdateSources(); } #endregion } } |