■ DataGridControl 엘리먼트에서 추가 작업을 처리하는 방법을 보여준다.
▶ Employee.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 |
namespace TestProject { /// <summary> /// 직원 /// </summary> public class Employee { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 이름 - FirstName /// <summary> /// 이름 /// </summary> public string FirstName { get; set; } #endregion #region 성 - LastName /// <summary> /// 성 /// </summary> public string LastName { get; set; } #endregion #region 직업 - Occupation /// <summary> /// 직업 /// </summary> public string Occupation { get; set; } #endregion } } |
▶ EmployeeCollection.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 |
using System.Collections.ObjectModel; namespace TestProject { /// <summary> /// 직원 컬렉션 /// </summary> public class EmployeeCollection : ObservableCollection<Employee> { } } |
▶ 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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase" xmlns:xcdg="http://schemas.xceed.com/wpf/xaml/datagrid" xmlns:local="clr-namespace:TestProject" Width="600" Height="450" Title="추가 프로세스 수동 처리하기"> <Window.Resources> <local:EmployeeCollection x:Key="EmployeeObservableCollectionKey"> <local:Employee FirstName="Jenny" LastName="Beland" Occupation="Writer" /> <local:Employee FirstName="Francois" LastName="Carignan" Occupation="Developer" /> <local:Employee FirstName="Pascal" LastName="Bourque" Occupation="Developer" /> <local:Employee FirstName="Michel" LastName="Fortin" Occupation="Developer" /> <local:Employee FirstName="Marc" LastName="Laroche" Occupation="Developer" /> <local:Employee FirstName="Pierre-Luc" LastName="Ledoux" Occupation="Developer" /> <local:Employee FirstName="Mathieu" LastName="Drimonakos" Occupation="TechnicalSupport" /> <local:Employee FirstName="Catherine" LastName="Sauzede" Occupation="Infograph" /> </local:EmployeeCollection> <xcdg:DataGridCollectionViewSource x:Key="DataGridCollectionViewSourceKey" ItemType="{x:Type local:Employee}" Source="{StaticResource EmployeeObservableCollectionKey}" CreatingNewItem="dataGridCollectionViewSource_CreatingNewItem" CommittingNewItem="dataGridCollectionViewSource_CommittingNewItem" CancelingNewItem="dataGridCollectionViewSource_CancelingNewItem" /> </Window.Resources> <Grid> <xcdg:DataGridControl x:Name="dataGridControl" ItemsSource="{Binding Source={StaticResource DataGridCollectionViewSourceKey}}"> <xcdg:DataGridControl.View> <xcdg:TableView> <xcdg:TableView.FixedHeaders> <DataTemplate> <xcdg:InsertionRow /> </DataTemplate> </xcdg:TableView.FixedHeaders> </xcdg:TableView> </xcdg:DataGridControl.View> </xcdg:DataGridControl> </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 72 73 74 75 76 77 78 79 80 81 82 83 |
using System.Windows; using Xceed.Wpf.DataGrid; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 데이터 그리드 컬렉션 뷰 소스 새 항목 생성시 처리하기 - dataGridCollectionViewSource_CreatingNewItem(sender, e) /// <summary> /// 데이터 그리드 컬렉션 뷰 소스 새 항목 생성시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void dataGridCollectionViewSource_CreatingNewItem(object sender, DataGridCreatingNewItemEventArgs e) { e.NewItem = new Employee(); e.Handled = true; } #endregion #region 데이터 그리드 컬렉션 뷰 소스 새 항목 커밋시 처리하기 - dataGridCollectionViewSource_CommittingNewItem(sender, e) /// <summary> /// 데이터 그리드 컬렉션 뷰 소스 새 항목 커밋시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void dataGridCollectionViewSource_CommittingNewItem(object sender, DataGridCommittingNewItemEventArgs e) { EmployeeCollection collection = e.CollectionView.SourceCollection as EmployeeCollection; collection.Add((Employee)e.Item); e.Index = collection.Count - 1; e.NewCount = collection.Count; e.Handled = true; } #endregion #region 데이터 그리드 컬렉션 뷰 소스 새 항목 취소시 처리하기 - dataGridCollectionViewSource_CancelingNewItem(sender, e) /// <summary> /// 데이터 그리드 컬렉션 뷰 소스 새 항목 취소시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void dataGridCollectionViewSource_CancelingNewItem(object sender, DataGridItemHandledEventArgs e) { e.Handled = true; } #endregion } } |