[C#/WPF] DataGrid 엘리먼트 : RowDetailsTemplate 속성 사용하기
■ DataGrid 엘리먼트의 RowDetailsTemplate 속성을 사용하는 방법을 보여준다. ▶ Person.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 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 |
using System.ComponentModel; namespace TestProject { /// <summary> /// 사람 /// </summary> public class Person : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 이벤트 - PropertyChanged /// <summary> /// 속성 변경시 이벤트 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 회사명 /// <summary> private string companyName; /// <summary> /// 이름 /// <summary> private string firstName; /// <summary> /// 성 /// <summary> private string lastName; /// <summary> /// 이메일 주소 /// <summary> private string emailAddress; /// <summary> /// 전화 /// <summary> private string phone; /// <summary> /// 영업 담당 /// <summary> private string salesPerson; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 회사명 - CompanyName /// <summary> /// 회사명 /// </summary> public string CompanyName { get { return this.companyName; } set { if(this.companyName == value) { return; } this.companyName = value; FirePropertyChangedEvent(nameof(CompanyName)); } } #endregion #region 이름 - FirstName /// <summary> /// 이름 /// </summary> public string FirstName { get { return this.firstName; } set { if(this.firstName == value) { return; } this.firstName = value; FirePropertyChangedEvent(nameof(FirstName)); } } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> public string LastName { get { return this.lastName; } set { if(this.lastName == value) { return; } this.lastName = value; FirePropertyChangedEvent(nameof(LastName)); } } #endregion #region 이메일 주소 - EmailAddress /// <summary> /// 이메일 주소 /// </summary> public string EmailAddress { get { return this.emailAddress; } set { if(this.emailAddress == value) { return; } this.emailAddress = value; FirePropertyChangedEvent(nameof(EmailAddress)); } } #endregion #region 전화 - Phone /// <summary> /// 전화 /// </summary> public string Phone { get { return this.phone; } set { if(this.phone == value) { return; } this.phone = value; FirePropertyChangedEvent(nameof(Phone)); } } #endregion #region 영업 담당 - SalesPerson /// <summary> /// 영업 담당 /// </summary> public string SalesPerson { get { return this.salesPerson; } set { if(this.salesPerson == value) { return; } this.salesPerson = value; FirePropertyChangedEvent(nameof(SalesPerson)); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Protected #region 속성 변경시 이벤트 발생시키기 - FirePropertyChangedEvent(propertyName) /// <summary> /// 속성 변경시 이벤트 발생시키기 /// </summary> /// <param name="propertyName">속성명</param> protected void FirePropertyChangedEvent(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="800" Height="600" Title="DataGrid 엘리먼트 : RowDetailsTemplate 속성 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Margin="10"> <DataGrid Name="dataGrid" AutoGenerateColumns="False" IsReadOnly="True"> <DataGrid.Columns> <DataGridTextColumn Header="회사명" Binding="{Binding CompanyName}" /> <DataGridTextColumn Header="이름" Binding="{Binding FirstName}" /> <DataGridTextColumn Header="성" Binding="{Binding LastName}" /> </DataGrid.Columns> <DataGrid.RowDetailsTemplate> <DataTemplate> <Border BorderThickness="0" Background="BlanchedAlmond" Padding="10"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Text="메일 주소" /> <TextBlock Grid.Row="0" Grid.Column="1" Margin="10 0 0 0" VerticalAlignment="Center" Foreground="MidnightBlue" Text="{Binding EmailAddress}" /> <TextBlock Grid.Row="1" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Text="전화" /> <TextBlock Grid.Row="1" Grid.Column="1" Margin="10 0 0 0" VerticalAlignment="Center" Foreground="MidnightBlue" Text="{Binding Phone}" /> <TextBlock Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" VerticalAlignment="Center" Text="영업 담당자" /> <TextBlock Grid.Row="2" Grid.Column="1" Margin="10 0 0 0" VerticalAlignment="Center" Foreground="MidnightBlue" Text="{Binding SalesPerson}" /> </Grid> </Border> </DataTemplate> </DataGrid.RowDetailsTemplate> </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 70 71 |
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<Person> collection = new ObservableCollection<Person>(); collection.Add ( new Person { CompanyName = "회사1", FirstName = "길동", LastName = "홍", EmailAddress = "id01@abc.com", Phone = "000-0000-0000", SalesPerson = "김철수" } ); collection.Add ( new Person { CompanyName = "회사1", FirstName = "동구", LastName = "강", EmailAddress = "id02@abc.com", Phone = "000-0000-0000", SalesPerson = "김철수" } ); collection.Add ( new Person { CompanyName = "회사2", FirstName = "희수", LastName = "김", EmailAddress = "id03@abc.com", Phone = "000-0000-0000", SalesPerson = "이영희" } ); this.dataGrid.ItemsSource = collection; } #endregion } } |
TestProject.zip