■ DataGrid 엘리먼트에서 커스텀 컬럼을 사용하는 방법을 보여준다.
▶ OrderStatus.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 |
namespace TestProject { /// <summary> /// 주문 상태 /// </summary> public enum OrderStatus { /// <summary> /// 해당 무 /// </summary> None, /// <summary> /// 신규 /// </summary> New, /// <summary> /// 처리중 /// </summary> Processing, /// <summary> /// 배송 /// </summary> Shipped, /// <summary> /// 수신 /// </summary> Received }; } |
▶ 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 |
using System; namespace TestProject { /// <summary> /// 고객 /// </summary> public class Customer { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이름 - FirstName /// <summary> /// 이름 /// </summary> public string FirstName { get; set; } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> public string LastName { get; set; } #endregion #region 이메일 - EMail /// <summary> /// 이메일 /// </summary> public Uri EMail { get; set; } #endregion #region 회원 여부 - IsMember /// <summary> /// 회원 여부 /// </summary> public bool IsMember { get; set; } #endregion #region 상태 - Status /// <summary> /// 상태 /// </summary> public OrderStatus Status { get; set; } #endregion } } |
▶ EMailConverter.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 |
using System; using System.Globalization; using System.Windows.Data; namespace TestProject { /// <summary> /// 이메일 변환자 /// </summary> public class EMailConverter : IValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(sourceValue, targetType, parameter, cultureInfo) /// <summary> /// 변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>변환 값</returns> public object Convert(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo) { if(sourceValue != null) { string email = sourceValue.ToString(); int index = email.IndexOf("@"); string alias = email.Substring(7, index - 7); return alias; } else { return string.Empty; } } #endregion #region 역변환하기 - ConvertBack(sourceValue, targetType, parameter, cultureInfo) /// <summary> /// 역변환하기 /// </summary> /// <param name="sourceValue">소스 값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">문화 정보</param> /// <returns>역변환 값</returns> public object ConvertBack(object sourceValue, Type targetType, object parameter, CultureInfo cultureInfo) { Uri uri = new Uri((string)sourceValue); return uri; } #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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:s="clr-namespace:System;assembly=mscorlib" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="DataGrid 엘리먼트 : 커스텀 컬럼 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:EMailConverter x:Key="EMailConverterKey" /> <ObjectDataProvider x:Key="EnumerationObjectDataProviderKey" ObjectType="{x:Type s:Enum}" MethodName="GetValues"> <ObjectDataProvider.MethodParameters> <x:Type Type="local:OrderStatus" /> </ObjectDataProvider.MethodParameters> </ObjectDataProvider> </Window.Resources> <Grid Margin="10"> <DataGrid Name="dataGrid" AutoGenerateColumns="False" CanUserAddRows="False" CanUserDeleteRows="False"> <DataGrid.Columns> <DataGridTextColumn Header="이름" Binding="{Binding FirstName}"/> <DataGridTextColumn Header="성" Binding="{Binding LastName}" /> <DataGridHyperlinkColumn Header="이메일" Binding="{Binding EMail}" ContentBinding="{Binding EMail, Converter={StaticResource EMailConverterKey}}" /> <DataGridCheckBoxColumn Header="회원 여부" Binding="{Binding IsMember}" /> <DataGridComboBoxColumn Header="주문 상태" SelectedItemBinding="{Binding Status}" ItemsSource="{Binding Source={StaticResource EnumerationObjectDataProviderKey}}" /> </DataGrid.Columns> </DataGrid> </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 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
using System; using System.Collections.ObjectModel; using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); ObservableCollection<Customer> collection = new ObservableCollection<Customer>(); collection.Add ( new Customer { FirstName = "길동", LastName = "홍", EMail = new Uri("mailto:hong@daum.com"), IsMember = true, Status = OrderStatus.Processing } ); collection.Add ( new Customer { FirstName = "영희", LastName = "김", EMail = new Uri("mailto:kim@naver.com"), IsMember = false, Status = OrderStatus.New } ); collection.Add ( new Customer { FirstName = "장수", LastName = "민", EMail = new Uri("mailto:min@naver.com"), IsMember = true, Status = OrderStatus.Shipped } ); this.dataGrid.ItemsSource = collection; } #endregion } } |