■ GridControl 클래스에서 행 선택을 구현하는 방법을 보여준다.
▶ 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 70 71 72 73 74 75 76 77 78 79 80 81 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid" Width="800" Height="600" Title="GridControl 클래스 : 행 선택 구현하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.Resources> <DataTemplate x:Key="CellDataTemplateKey"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image x:Name="image" Grid.Column="0" Margin="1" Stretch="None" Source="Image/cart.png" /> <TextBlock Grid.Column="1" Margin="1" Text="{Binding Path=Value}" /> </Grid> <DataTemplate.Triggers> <DataTrigger Binding="{Binding Path=Data.Selected}" Value="False"> <Setter TargetName="image" Property="Visibility" Value="Collapsed" /> </DataTrigger> </DataTemplate.Triggers> </DataTemplate> <DataTemplate x:Key="TotalSummaryDataTemplateKey"> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition Width="20" /> <ColumnDefinition Width="*" /> </Grid.ColumnDefinitions> <Image Grid.Column="0" Margin="1" Stretch="None" Source="Image/rectangle.png" /> <TextBlock Grid.Column="1" Margin="1" Text="{Binding Path=Column.TotalSummaryText}" /> </Grid> </DataTemplate> </Grid.Resources> <dxg:GridControl x:Name="gridControl" CustomUnboundColumnData="gridControl_CustomUnboundColumnData"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="ProductName" CellTemplate="{StaticResource CellDataTemplateKey}" /> <dxg:GridColumn FieldName="UnitPrice" /> <dxg:GridColumn FieldName="Selected" UnboundType="Boolean" /> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TableView x:Name="tableView" ShowGroupPanel="False" NavigationStyle="Row" ShowTotalSummary="True" TotalSummaryItemTemplate="{StaticResource TotalSummaryDataTemplateKey}" MouseDoubleClick="tableView_MouseDoubleClick" /> </dxg:GridControl.View> <dxg:GridControl.TotalSummary> <dxg:GridSummaryItem FieldName="UnitPrice" SummaryType="Custom" /> </dxg:GridControl.TotalSummary> </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 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 |
using System; using System.Collections.Generic; using System.Windows; using System.Windows.Input; using DevExpress.Data; using DevExpress.Xpf.Grid; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 언바운드 데이터 리스트 /// </summary> private List<bool> unboundDataList = new List<bool>(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); Loaded += Window_Loaded; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { this.gridControl.ItemsSource = new NorthwindEntities().Products; SetUnboundDataSource(this.gridControl.VisibleRowCount); this.gridControl.CustomSummary += gridControl_CustomSummary; } #endregion #region 그리드 컨트롤 커스텀 요약 처리하기 - gridControl_CustomSummary(sender, e) /// <summary> /// 그리드 컨트롤 커스텀 요약 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void gridControl_CustomSummary(object sender, CustomSummaryEventArgs e) { if(e.SummaryProcess == CustomSummaryProcess.Start) { e.TotalValue = 0m; } if(e.SummaryProcess == CustomSummaryProcess.Calculate) { if((bool)e.GetValue("Selected")) { e.TotalValue = Convert.ToDecimal(e.TotalValue) + Convert.ToDecimal(e.GetValue("UnitPrice")); } } } #endregion #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") { if(e.IsGetData) { e.Value = this.unboundDataList[e.ListSourceRowIndex]; } if(e.IsSetData) { this.unboundDataList[e.ListSourceRowIndex] = (bool)e.Value; } } } #endregion #region 테이블 뷰 마우스 더블 클릭시 처리하기 - tableView_MouseDoubleClick(sender, e) /// <summary> /// 테이블 뷰 마우스 더블 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void tableView_MouseDoubleClick(object sender, MouseButtonEventArgs e) { int rowHandle = this.gridControl.View.GetRowHandleByMouseEventArgs(e); if(this.gridControl.IsValidRowHandle(rowHandle)) { this.gridControl.SetCellValue(rowHandle, "Selected", !(bool)this.gridControl.GetCellValue(rowHandle, "Selected")); this.gridControl.RefreshData(); } } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 언바운드 데이터 소스 설정하기 - SetUnboundDataSource(count) /// <summary> /// 언바운드 데이터 소스 설정하기 /// </summary> /// <param name="count">카운트</param> private void SetUnboundDataSource(int count) { for(int i = 0; i < count; i++) { this.unboundDataList.Add(false); } } #endregion } } |