[C#/SILVERLIGHT] Explorer 새 윈도우 열기
■ Explorer 새 윈도우를 여는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 |
using System.Windows.Browser; HtmlPage.Window.Navigate(new Uri("http://www.naver.com", UriKind.Absolute), "_blank"); |
■ Explorer 새 윈도우를 여는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 |
using System.Windows.Browser; HtmlPage.Window.Navigate(new Uri("http://www.naver.com", UriKind.Absolute), "_blank"); |
■ Explorer 팝업 윈도우를 여는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System.Windows.Browser; HtmlPopupWindowOptions htmlPopupWindowOptions = new HtmlPopupWindowOptions(); htmlPopupWindowOptions.Left = 0; htmlPopupWindowOptions.Top = 0; htmlPopupWindowOptions.Width = 800; htmlPopupWindowOptions.Height = 600; htmlPopupWindowOptions.Menubar = false; htmlPopupWindowOptions.Toolbar = false; if(HtmlPage.IsPopupWindowAllowed) { HtmlPage.PopupWindow(new Uri("http://www.naver.com"), "new", htmlPopupWindowOptions); } |
■ DataGrid 엘리먼트에서 데이터 주석 어트리뷰트를 사용해 컬럼 속성을 설정하는 방법을 보여준다. ▶ ProductColor.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 |
using System.ComponentModel.DataAnnotations; /// <summary> /// 제품 색상 /// </summary> public enum ProductColor { /// <summary> /// 빨강 /// </summary> Red, /// <summary> /// 흰색 /// </summary> White, /// <summary> /// 보라 /// </summary> Purple, /// <summary> /// 파랑 /// </summary> Blue } |
▶ Product.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 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 95 96 97 98 99 100 101 |
using System.ComponentModel.DataAnnotations; /// <summary> /// 제품 /// </summary> public class Product { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제품 ID - ProductID /// <summary> /// 제품 ID /// </summary> [Display(Name="Product Number")] [Range(0, 5000)] public int ProductID { get; set; } #endregion #region 제품명 - ProductName /// <summary> /// 제품명 /// </summary> [Display(Name="Name")] [Required] public string ProductName { get; set; } #endregion #region 리스트 가격 - ListPrice /// <summary> /// 리스트 가격 /// </summary> [Display(Name="Price")] [DataType(DataType.Currency)] public double ListPrice { get; set; } #endregion #region 제품 색상 - ProductColor /// <summary> /// 제품 색상 /// </summary> [Display(Name="Color")] [EnumDataType(typeof(ProductColor))] public ProductColor ProductColor { get; set; } #endregion #region 재고 여부 - InStock /// <summary> /// 재고 여부 /// </summary> [Display(Name="Available")] public bool InStock { get; set; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Product() /// <summary> /// 생성자 /// </summary> public Product() { } #endregion #region 생성자 - Product(productID, productName, listPrice, productColor, inStock) /// <summary> /// 생성자 /// </summary> /// <param name="productID">제품 ID</param> /// <param name="productName">제품명</param> /// <param name="listPrice">리스트 가격</param> /// <param name="productColor">제품 색상</param> /// <param name="inStock">재고 여부</param> public Product(int productID, string productName, double listPrice, ProductColor productColor, bool inStock) { ProductID = productID; ProductName = productName; ListPrice = listPrice; ProductColor = productColor; InStock = inStock; } #endregion } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<Grid xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> <StackPanel Width="600" Height="450"> <TextBlock Text="Products" /> <sdk:DataGrid x:Name="dataGrid" Foreground="Black" AutoGenerateColumns="True"> </sdk:DataGrid> </StackPanel> </Grid> |
※
■ DescriptionViewer 엘리먼트의 Target 속성을 사용하는 방법을 보여준다. ▶ Customer.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 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
using System.ComponentModel.DataAnnotations; /// <summary> /// 고객 /// </summary> public class Customer { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// ID 번호 /// </summary> private int idNumber; /// <summary> /// 이름 /// </summary> private string firstName; /// <summary> /// 성 /// </summary> private string lastName; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID 번호 - IDNumber /// <summary> /// ID 번호 /// </summary> [Display(Name = "ID Number", Description = "Enter an integer between 0 and 99999.")] [Range(0, 99999)] public int IDNumber { get { return this.idNumber; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "IDNumber" }); this.idNumber = value; } } #endregion #region 이름 - FirstName /// <summary> /// 이름 /// </summary> [Display(Name = "Name", Description = "First Name + Last Name.")] [Required(ErrorMessage = "First Name is required.")] [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numbers and special characters are not allowed in the name.")] public string FirstName { get { return this.firstName; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "FirstName" }); this.firstName = value; } } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> [Required(ErrorMessage = "Last Name is required.")] [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numbers and special characters are not allowed in the name.")] [StringLength(8, MinimumLength = 3, ErrorMessage = "Last name must be between 3 and 8 characters long.")] public string LastName { get { return this.lastName; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "LastName" }); this.lastName = value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Customer(idNumber, firstName, lastName) /// <summary> /// 생성자 /// </summary> /// <param name="idNumber">ID 번호</param> /// <param name="firstName">이름</param> /// <param name="lastName">성</param> public Customer(int idNumber, string firstName, string lastName) { IDNumber = idNumber; FirstName = firstName; LastName = lastName; } #endregion } |
▶ MainPage.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 |
<Grid x:Name="grid" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" Margin="15"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="300" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="35" /> <RowDefinition Height="35" /> <RowDefinition Height="35" /> <RowDefinition Height="100"/> </Grid.RowDefinitions> <sdk:Label Grid.Row="0" Grid.Column="0" Margin="5" IsRequired="True" Content="Date of Birth" /> <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal"> <sdk:DatePicker Height="23" /> <sdk:DescriptionViewer Description="Please enter your date of birth." /> </StackPanel> <sdk:Label Grid.Row="1" Grid.Column="0" Margin="5" Target="{Binding ElementName=idNumberTextBox}" /> <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal"> <TextBox x:Name="idNumberTextBox" Height="23" Width="100" Text="{Binding IDNumber, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> <sdk:DescriptionViewer Target="{Binding ElementName=idNumberTextBox}" /> </StackPanel> <sdk:Label Grid.Row="2" Grid.Column="0" Margin="5" Target="{Binding ElementName=nameStackPanel}" PropertyPath="FirstName" /> <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal"> <StackPanel x:Name="nameStackPanel" Orientation="Horizontal"> <TextBox x:Name="firstNameTextBox" Width="100" Height="23" Text="{Binding FirstName, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> <TextBox x:Name="lastNameTextBox" Width="100" Height="23" Text="{Binding LastName, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> </StackPanel> <sdk:DescriptionViewer Target="{Binding ElementName=nameStackPanel}" PropertyPath="FirstName" /> </StackPanel> <sdk:ValidationSummary Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" /> </Grid> |
※ System.Windows.Controls 참조를 추가한다. ※ System.Windows.Controls.Data.Input 참조를 추가한다.
■ IsolatedStorageFile 클래스를 사용해 격리된 저장소 할당량을 증가시키는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System.IO.IsolatedStorage; using(IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { long spaceToAdd = 5242880L; // 5 메가 바이트 long availableFreeSpace = isolatedStorageFile.AvailableFreeSpace; if(availableFreeSpace < spaceToAdd) { if(!isolatedStorageFile.IncreaseQuotaTo(isolatedStorageFile.Quota + spaceToAdd)) { // 사용자가 승인하지 않는 경우 } else { // 사용자가 승인한 경우 } } } |
■ IsolatedStorageFile 클래스를 사용해 격리된 저장소를 사용하는 방법을 보여준다. ———————————————————————————————————————— using System.IO.IsolatedStorage; IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication(); ————————————————————————————————————————
■ IsolatedStorageFile 클래스를 사용해 격리된 저장소와 그 안에 있는 디렉토리와 파일을 영구 제거하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 |
using System.IO.IsolatedStorage; using(IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { isolatedStorageFile.Remove(); } |
■ IsolatedStorageFile 클래스를 사용해 파일 배열을 구하는 방법을 보여준다. ▶ IsolatedStorageFile 클래스 : 파일 배열 구하기 예제 (C#)
1 2 3 4 5 6 7 8 9 |
using System.IO.IsolatedStorage; using(IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { string[] fileArray1 = GetFileArray(isolatedStorageFile, string.Empty); // 루트 폴더에서 파일 배열을 구한다. string[] fileArray2 = GetFileArray(isolatedStorageFile, @"test\*.*" ); // test 폴더에서 파일 배열을 구한다. } |
▶ IsolatedStorageFile 클래스
■ IsolatedStorageFile 클래스를 사용해 파일을 삭제하는 방법을 보여준다. ▶ IsolatedStorageFile 클래스 : 파일 삭제하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System.IO.IsolatedStorage; using(IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { string filePath = @"test\sample.txt"; if(isolatedStorageFile.FileExists(filePath)) { DeleteFile(isolatedStorageFile, filePath); // test 폴더에서 sample.txt를 삭제한다. } } |
▶ IsolatedStorageFile 클래스 : 파일
■ IsolatedStorageFile 클래스를 사용해 파일을 생성하는 방법을 보여준다. ▶ IsolatedStorageFile 클래스 : 파일 생성하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
using System.IO.IsolatedStorage; using(IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { // sample.txt 파일을 생성한다. IsolatedStorageFileStream isolatedStorageFileStream1 = CreateFile(isolatedStorageFile, "sample.txt"); // test 폴더에서 sample.txt 파일을 생성한다. IsolatedStorageFileStream isolatedStorageFileStream2 = CreateFile(isolatedStorageFile, @"test\sample.txt")); ... isolatedStorageFileStream1.Close(); isolatedStorageFileStream2.Close(); } |
▶ IsolatedStorageFile 클래스 : 파일
■ IsolatedStorageFile 클래스를 사용해 파일 존재 여부를 조사하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System.IO.IsolatedStorage; using(IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { string filePath = @"test\sample.txt"; if(isolatedStorageFile.FileExists(filePath)) // test 폴더에서 sample.txt 파일 존재 여부를 조사한다. { ... } } |
■ IsolatedStorageFile 클래스를 사용해 파일 경로 배열을 구하는 방법을 보여준다. ▶ IsolatedStorageFile 클래스 : 파일 경로 배열 구하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 |
using System.IO.IsolatedStorage; using(IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { string[] filePathArray1 = GetFilePathArray(isolatedStorageFile, string.Empty); // 루트 폴더에서 파일 경로 배열을 구한다. string[] filePathArray2 = GetFilePathArray(isolatedStorageFile, "test\*.*"); // test 폴더에서 파일 경로 배열을 구한다. } |
▶
■ IsolatedStorageFile 클래스를 사용해 디렉토리를 삭제하는 방법을 보여준다. ▶ IsolatedStorageFile 클래스 : 디렉토리 삭제하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System.IO.IsolatedStorage; using(IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { string directoryPath = "test"; if(isolatedStorageFile.DirectoryExists(directoryPath)) { DeleteFolder(isolatedStorageFile, directoryPath); // test 폴더를 삭제한다. } } |
▶ IsolatedStorageFile 클래스 : 디렉토리
■ IsolatedStorageFile 클래스를 사용해 디렉토리를 생성하는 방법을 보여준다. ▶ IsolatedStorageFile 클래스 : 디렉토리 생성하기 예제 (C#)
1 2 3 4 5 6 7 8 9 10 |
using System.IO.IsolatedStorage; using(IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { CreateDirectory(isolatedStorageFile, "test"); // test 디렉토리를 생성한다. CreateDirectory(isolatedStorageFile, @"test\sample"); // test 디렉토리에서 sample 디렉토리를 생성한다. } |
▶ IsolatedStorageFile 클래스 : 디렉토리
■ IsolatedStorageFile 클래스를 사용해 디렉토리 존재 여부를 조사하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System.IO.IsolatedStorage; using(IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { string directoryPath = "test"; if(isolatedStorageFile.DirectoryExists(directoryPath)) // test 디렉토리 존재 여부를 조사한다. { ... } } |
■ IsolatedStorageFile 클래스를 사용해 할당량을 조사하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 6 7 8 9 10 11 12 |
using System.IO.IsolatedStorage; using(IsolatedStorageFile isolatedStorageFile = IsolatedStorageFile.GetUserStoreForApplication()) { long totalSpace = isolatedStorageFile.Quota; long availableSpace = isolatedStorageFile.AvailableFreeSpace; long usedSpace = totalSpace - availableSpace; ... } |
■ IsolatedStorageSettings 클래스를 사용해 설정을 구하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 |
using System.IO.IsolatedStorage; string eMailAddress = IsolatedStorageSettings.ApplicationSettings["EMailAddress"] as string; |
■ IsolatedStorageSettings 클래스를 사용해 설정을 변경하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 |
string eMailAddress = "icodebroker@naver.com"; IsolatedStorageSettings.ApplicationSettings["EMailAddress"] = eMailAddress; |
■ IsolatedStorageSettings 클래스를 사용해 설정을 삭제하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 |
using System.IO.IsolatedStorage; IsolatedStorageSettings.ApplicationSettings.Remove("EMailAddress"); |
■ IsolatedStorageSettings 클래스를 사용해 설정을 추가하는 방법을 보여준다. ▶ 예제 코드 (C#)
1 2 3 4 5 |
using System.IO.IsolatedStorage; IsolatedStorageSettings.ApplicationSettings.Add("EMailAddress", "icodebroker@naver.com"); |
■ Label 엘리먼트의 IsRequired 속성을 사용하는 방법을 보여준다. ▶ 예제 코드 (XAML)
1 2 3 4 5 6 7 |
<Grid xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk"> <sdk:Label IsRequired="True" Content="Date of Birth" /> </Grid> |
※ System.Windows.Controls.Data.Input 참조를 추가한다.
■ Label 엘리먼트의 Target 속성을 사용하는 방법을 보여준다. ▶ Customer.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 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
using System.ComponentModel.DataAnnotations; /// <summary> /// 고객 /// </summary> public class Customer { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// ID 번호 /// </summary> private int idNumber; /// <summary> /// 이름 /// </summary> private string firstName; /// <summary> /// 성 /// </summary> private string lastName; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID 번호 - IDNumber /// <summary> /// ID 번호 /// </summary> [Display(Name = "ID Number", Description = "Enter an integer between 0 and 99999.")] [Range(0, 99999)] public int IDNumber { get { return this.idNumber; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "IDNumber" }); this.idNumber = value; } } #endregion #region 이름 - FirstName /// <summary> /// 이름 /// </summary> [Display(Name = "Name", Description = "First Name + Last Name.")] [Required(ErrorMessage = "First Name is required.")] [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numbers and special characters are not allowed in the name.")] public string FirstName { get { return this.firstName; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "FirstName" }); this.firstName = value; } } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> [Required(ErrorMessage = "Last Name is required.")] [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numbers and special characters are not allowed in the name.")] [StringLength(8, MinimumLength = 3, ErrorMessage = "Last name must be between 3 and 8 characters long.")] public string LastName { get { return this.lastName; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "LastName" }); this.lastName = value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Customer(idNumber, firstName, lastName) /// <summary> /// 생성자 /// </summary> /// <param name="idNumber">ID 번호</param> /// <param name="firstName">이름</param> /// <param name="lastName">성</param> public Customer(int idNumber, string firstName, string lastName) { IDNumber = idNumber; FirstName = firstName; LastName = lastName; } #endregion } |
▶ MainPage.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 |
<Grid x:Name="grid" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" Margin="15"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="300" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="35" /> <RowDefinition Height="35" /> <RowDefinition Height="35" /> <RowDefinition Height="100"/> </Grid.RowDefinitions> <sdk:Label Grid.Row="0" Grid.Column="0" Margin="5" IsRequired="True" Content="Date of Birth" /> <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal"> <sdk:DatePicker Height="23" /> <sdk:DescriptionViewer Description="Please enter your date of birth." /> </StackPanel> <sdk:Label Grid.Row="1" Grid.Column="0" Margin="5" Target="{Binding ElementName=idNumberTextBox}" /> <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal"> <TextBox x:Name="idNumberTextBox" Width="100" Height="23" Text="{Binding IDNumber, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> <sdk:DescriptionViewer Target="{Binding ElementName=idNumberTextBox}" /> </StackPanel> <sdk:Label Grid.Row="2" Grid.Column="0" Margin="5" Target="{Binding ElementName=nameStackPanel}" PropertyPath="FirstName" /> <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal"> <StackPanel x:Name="nameStackPanel" Orientation="Horizontal"> <TextBox x:Name="firstNameTextBox" Width="100" Height="23" Text="{Binding FirstName, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> <TextBox x:Name="lastNameTextBox" Width="100" Height="23" Text="{Binding LastName, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> </StackPanel> <sdk:DescriptionViewer Target="{Binding ElementName=nameStackPanel}" PropertyPath="FirstName" /> </StackPanel> </Grid> |
※ System.Windows.Controls 참조를 추가한다. ※ System.Windows.Controls.Data.Input 참조를 추가한다.
■ ValidationSummary 엘리먼트에서 데이터 주석 어트리뷰트를 사용해 검증하는 방법을 보여준다. ▶ Customer.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 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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
using System.ComponentModel.DataAnnotations; /// <summary> /// 고객 /// </summary> public class Customer { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// ID 번호 /// </summary> private int idNumber; /// <summary> /// 이름 /// </summary> private string firstName; /// <summary> /// 성 /// </summary> private string lastName; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region ID 번호 - IDNumber /// <summary> /// ID 번호 /// </summary> [Display(Name = "ID Number", Description = "Enter an integer between 0 and 99999.")] [Range(0, 99999)] public int IDNumber { get { return this.idNumber; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "IDNumber" }); this.idNumber = value; } } #endregion #region 이름 - FirstName /// <summary> /// 이름 /// </summary> [Display(Name = "Name", Description = "First Name + Last Name.")] [Required(ErrorMessage = "First Name is required.")] [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numbers and special characters are not allowed in the name.")] public string FirstName { get { return this.firstName; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "FirstName" }); this.firstName = value; } } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> [Required(ErrorMessage = "Last Name is required.")] [RegularExpression(@"^[a-zA-Z''-'\s]{1,40}$", ErrorMessage = "Numbers and special characters are not allowed in the name.")] [StringLength(8, MinimumLength = 3, ErrorMessage = "Last name must be between 3 and 8 characters long.")] public string LastName { get { return this.lastName; } set { Validator.ValidateProperty(value, new ValidationContext(this, null, null) { MemberName = "LastName" }); this.lastName = value; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - Customer(idNumber, firstName, lastName) /// <summary> /// 생성자 /// </summary> /// <param name="idNumber">ID 번호</param> /// <param name="firstName">이름</param> /// <param name="lastName">성</param> public Customer(int idNumber, string firstName, string lastName) { IDNumber = idNumber; FirstName = firstName; LastName = lastName; } #endregion } |
▶ MainPage.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 |
<Grid x:Name="grid" xmlns:sdk="http://schemas.microsoft.com/winfx/2006/xaml/presentation/sdk" Margin="15"> <Grid.ColumnDefinitions> <ColumnDefinition Width="100" /> <ColumnDefinition Width="300" /> </Grid.ColumnDefinitions> <Grid.RowDefinitions> <RowDefinition Height="35" /> <RowDefinition Height="35" /> <RowDefinition Height="35" /> <RowDefinition Height="100"/> </Grid.RowDefinitions> <sdk:Label Grid.Row="0" Grid.Column="0" Margin="5" IsRequired="True" Content="Date of Birth" /> <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Horizontal"> <sdk:DatePicker Height="23" /> <sdk:DescriptionViewer Description="Please enter your date of birth." /> </StackPanel> <sdk:Label Grid.Row="1" Grid.Column="0" Margin="5" Target="{Binding ElementName=idNumberTextBox}" /> <StackPanel Grid.Row="1" Grid.Column="1" Orientation="Horizontal"> <TextBox x:Name="idNumberTextBox" Height="23" Width="100" Text="{Binding IDNumber, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> <sdk:DescriptionViewer Target="{Binding ElementName=idNumberTextBox}" /> </StackPanel> <sdk:Label Grid.Row="2" Grid.Column="0" Margin="5" Target="{Binding ElementName=nameStackPanel}" PropertyPath="FirstName" /> <StackPanel Grid.Row="2" Grid.Column="1" Orientation="Horizontal"> <StackPanel x:Name="nameStackPanel" Orientation="Horizontal"> <TextBox x:Name="firstNameTextBox" Width="100" Height="23" Text="{Binding FirstName, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> <TextBox x:Name="lastNameTextBox" Width="100" Height="23" Text="{Binding LastName, Mode=TwoWay, ValidatesOnExceptions=True, NotifyOnValidationError=True}" /> </StackPanel> <sdk:DescriptionViewer Target="{Binding ElementName=nameStackPanel}" PropertyPath="FirstName" /> </StackPanel> <sdk:ValidationSummary Grid.Row="3" Grid.Column="0" Grid.ColumnSpan="2" /> </Grid> |
※ System.Windows.Controls 참조를 추가한다. ※ System.Windows.Controls.Data.Input
■ Binding 클래스을 사용하는 방법을 보여준다. ▶ Dog.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/// <summary> /// 개 /// </summary> public class Dog { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 개 이름 - DogName /// <summary> /// 개 이름 /// </summary> public string DogName { get; set; } #endregion } |
▶ MainPage.xaml
1 2 3 4 5 6 7 |
<Grid x:Name="grid" Background="White"> <TextBlock x:Name="textBlock" Text="Test" /> </Grid> |
▶ MainPage.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
using System.Windows.Data; ... Dog dog = new Dog(); dog.DogName = "Spot"; ... Binding binding = new Binding(); binding.Path = new PropertyPath("DogName"); binding.Mode = BindingMode.OneTime; binding.Source = dog; // 또는 textBlock.DataContext = dog; ... this.textBlock.SetBinding(TextBlock.TextProperty, binding); |
■ Binding 태그 확장을 사용하는 방법을 보여준다. ▶ Dog.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
/// <summary> /// 개 /// </summary> public class Dog { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 개 이름 - DogName /// <summary> /// 개 이름 /// </summary> public string DogName { get; set; } #endregion } |
▶ MainPage.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<UserControl x:Class="TestProject.MainPage" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:TestSilverlightApplication" mc:Ignorable="d" d:DesignWidth="400" d:DesignHeight="300"> <Grid> <Grid.Resources> <local:Dog x:Key="DogKey" DogName="Spot" /> </Grid.Resources> <TextBlock Text="{Binding DogName, Source={StaticResource DogKey}, Mode=OneTime}" /> <!--TextBlock DataContext="{StaticResource DogKey}" Text="{Binding DogName, Mode=OneTime}" /--> </Grid> </UserControl> |