■ GridControl 클래스에서 언바운드 편집 가능한 체크 컬럼을 추가하는 방법을 보여준다.
▶ SelectionHelper.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 |
using System.Collections.Generic; using System.Text; namespace TestProject { /// <summary> /// 선택 도우미 /// </summary> /// <typeparam name="KeyType">키 타입</typeparam> public class SelectionHelper<KeyType> { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 사전 /// </summary> private Dictionary<KeyType, bool> dictionary = new Dictionary<KeyType, bool>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 선택 여부 구하기 - GetIsSelected(key) /// <summary> /// 선택 여부 구하기 /// </summary> /// <param name="key">키</param> /// <returns>선택 여부</returns> public bool GetIsSelected(KeyType key) { bool isSelected; if(this.dictionary.TryGetValue(key, out isSelected)) { return isSelected; } return false; } #endregion #region 선택 여부 설정하기 - SetIsSelected(key, value) /// <summary> /// 선택 여부 설정하기 /// </summary> /// <param name="key">키</param> /// <param name="value">값</param> public void SetIsSelected(KeyType key, bool value) { if(value) { this.dictionary[key] = value; } else { this.dictionary.Remove(key); } } #endregion #region 선택 키 리스트 구하기 - GetSelectedKeyList() /// <summary> /// 선택 키 리스트 구하기 /// </summary> /// <returns>선택 키 리스트</returns> public List<KeyType> GetSelectedKeyList() { List<KeyType> keyList = new List<KeyType>(); foreach(KeyType key in this.dictionary.Keys) { keyList.Add(key); } return keyList; } #endregion #region 선택 키 리스트에서 문자열 구하기 - GetStringFromSelectedKeyList() /// <summary> /// 선택 키 리스트에서 문자열 구하기 /// </summary> /// <returns>문자열</returns> public string GetStringFromSelectedKeyList() { List<KeyType> keyList = GetSelectedKeyList(); StringBuilder stringBuilder = new StringBuilder(); for(int i = 0; i < keyList.Count; i++) { stringBuilder.AppendLine(keyList[i].ToString()); } return stringBuilder.ToString(); } #endregion #region 선택 카운트 구하기 - GetSelectionCount() /// <summary> /// 선택 카운트 구하기 /// </summary> /// <returns>선택 카운트</returns> public int GetSelectionCount() { return this.dictionary.Count; } #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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxe="http://schemas.devexpress.com/winfx/2008/xaml/editors" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" Width="800" Height="600" Title="GridControl 클래스 : 언바운드 편집 가능한 체크 컬럼 구현하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Button Grid.Row="0" Content="Invert Selection" Click="invertSelectionButton_Click" /> <Button Grid.Row="1" Margin="0 10 0 0" Content="Get Selection" Click="getSelectionButton_Click" /> <dxg:GridControl x:Name="gridControl" Grid.Row="2" Margin="0 10 0 0" CustomUnboundColumnData="gridControl_CustomUnboundColumnData" ItemsSource="{Binding Data}"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="ID" /> <dxg:GridColumn FieldName="Number" /> <dxg:GridColumn FieldName="Selected" UnboundType="Boolean" AllowSorting="False"> <dxg:GridColumn.CellTemplate> <DataTemplate> <dxe:CheckEdit IsChecked="{Binding Data.Selected}" HorizontalAlignment="Center" VerticalAlignment="Center" /> </DataTemplate> </dxg:GridColumn.CellTemplate> </dxg:GridColumn> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView x:Name="tableView" /> </dxg:GridControl.View> </dxg:GridControl> </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 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 |
using System; using System.Windows; using DevExpress.Xpf.Grid; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 샘플 리스트 /// </summary> private SampleList sampleList = new SampleList(); /// <summary> /// 선택 도우미 /// </summary> private SelectionHelper<Guid> selectionHelper = new SelectionHelper<Guid>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 메인 윈도우 - MainWindow() /// <summary> /// 메인 윈도우 /// </summary> public MainWindow() { InitializeComponent(); DataContext = this.sampleList; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 그리드 컨트롤 커스텀 언바운드 컬럼 데이터 처리하기 - gridControl_CustomUnboundColumnData(sender, e) /// <summary> /// 그리드 컨트롤 커스텀 언바운드 컬럼 데이터 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridControl_CustomUnboundColumnData(object sender, GridColumnDataEventArgs e) { if(e.Column.FieldName != "Selected") { return; } Guid keyGuid = (Guid)e.GetListSourceFieldValue("ID"); if(e.IsGetData) { e.Value = this.selectionHelper.GetIsSelected(keyGuid); } if(e.IsSetData) { this.selectionHelper.SetIsSelected(keyGuid, (bool)e.Value); } } #endregion #region Invert Selection 버튼 클릭시 처리하기 - invertSelectionButton_Click(sender, e) /// <summary> /// Invert Selection 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void invertSelectionButton_Click(object sender, RoutedEventArgs e) { for(int i = 0; i < this.sampleList.Data.Count; i++) { int rowHandle = this.gridControl.GetRowHandleByListIndex(i); bool newIsSelected = !this.selectionHelper.GetIsSelected(this.sampleList.Data[i].ID); this.gridControl.SetCellValue(rowHandle, "Selected", newIsSelected); } } #endregion #region Get Selection 버튼 클릭시 처리하기 - getSelectionButton_Click(sender, e) /// <summary> /// Get Selection 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void getSelectionButton_Click(object sender, RoutedEventArgs e) { string caption = string.Format("Selected rows (Total: {0})", this.selectionHelper.GetSelectionCount()); MessageBox.Show(this.selectionHelper.GetStringFromSelectedKeyList(), caption); } #endregion } } |