■ 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> |
※ System.Windows.Controls.Data 참조를 추가한다.
▶ 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.Collections.ObjectModel; ... /// <summary> /// 제품 컬렉션 /// </summary> private ObservableCollection<Product> productCollection; ... this.productCollection = new ObservableCollection<Product>(); this.productCollection.Add(new Product(1, "Bike" , 500, ProductColor.Red , true )); this.productCollection.Add(new Product(2, "Chair", 250, ProductColor.White , true )); this.productCollection.Add(new Product(3, "Plate", 20 , ProductColor.Purple, false)); this.productCollection.Add(new Product(4, "Kite" , 15 , ProductColor.Blue , true )); this.dataGrid.ItemsSource = this.productCollection; |