■ 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 |
<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" xmlns:local="clr-namespace:TestProject" Width="800" Height="600" Title="셀 값을 편집하기 위해 커스터 에디터 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <local:ViewModel x:Key="ViewModelKey" /> <local:IntegerToDoubleValueConverter x:Key="IntegerToDoubleValueConverterKey" /> </Window.Resources> <Grid DataContext="{StaticResource ViewModelKey}"> <dxg:GridControl x:Name="gridControl" ItemsSource="{Binding ProductList}" CustomUnboundColumnData="gridControl_CustomUnboundColumnData"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="ProductName" /> <dxg:GridColumn FieldName="UnitPrice"> <dxg:GridColumn.EditSettings> <dxe:SpinEditSettings DisplayFormat="c2" MinValue="1" MaxValue="999" /> </dxg:GridColumn.EditSettings> </dxg:GridColumn> <dxg:GridColumn FieldName="OrderUnit"> <dxg:GridColumn.DisplayTemplate> <ControlTemplate> <ProgressBar Margin="2" Minimum="0" Maximum="50" Value="{Binding Path=DisplayText, Mode=OneWay, RelativeSource={RelativeSource TemplatedParent}}" /> </ControlTemplate> </dxg:GridColumn.DisplayTemplate> <dxg:GridColumn.EditTemplate> <ControlTemplate> <Grid VerticalAlignment="Center"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*" /> <ColumnDefinition Width="30" /> </Grid.ColumnDefinitions> <Slider x:Name="PART_Editor" Grid.Column="0" Minimum="0" Maximum="50" Value="{Binding Path=EditValue, Mode=TwoWay, RelativeSource={RelativeSource TemplatedParent}, Converter={StaticResource IntegerToDoubleValueConverterKey}}" /> <TextBlock Grid.Column="1" HorizontalAlignment="Center" VerticalAlignment="Center" Foreground="Black" TextWrapping="NoWrap" Text="{Binding EditValue, RelativeSource={RelativeSource TemplatedParent}}" /> </Grid> </ControlTemplate> </dxg:GridColumn.EditTemplate> </dxg:GridColumn> <dxg:GridColumn FieldName="Total" UnboundType="Decimal" ReadOnly="True"> <dxg:GridColumn.EditSettings> <dxe:TextEditSettings DisplayFormat="c2" /> </dxg:GridColumn.EditSettings> </dxg:GridColumn> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg: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 |
using System; using System.Windows; using DevExpress.Xpf.Grid; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #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 == "Total") { int unitPrice = Convert.ToInt32 (this.gridControl.GetCellValueByListIndex(e.ListSourceRowIndex, "UnitPrice")); double orderUnit = Convert.ToDouble(this.gridControl.GetCellValueByListIndex(e.ListSourceRowIndex, "OrderUnit")); e.Value = unitPrice * orderUnit; } } #endregion } } |
▶ IntegerToDoubleValueConverter.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 |
using System; using System.Globalization; using System.Windows.Data; namespace TestProject { /// <summary> /// 정수↔실수 값 변환자 /// </summary> public class IntegerToDoubleValueConverter : IValueConverter { //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 변환하기 - Convert(value, targetType, parameter, cultureInfo) /// <summary> /// 변환하기 /// </summary> /// <param name="value">값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">CultureInfo 객체</param> /// <returns>변환 값</returns> public object Convert(object value, Type targetType, object parameter, CultureInfo cultureInfo) { return System.Convert.ToDouble(value); } #endregion #region 역변환하기 - ConvertBack(value, targetType, parameter, cultureInfo) /// <summary> /// 역변환하기 /// </summary> /// <param name="value">값</param> /// <param name="targetType">타겟 타입</param> /// <param name="parameter">매개 변수</param> /// <param name="cultureInfo">CultureInfo 객체</param> /// <returns>역변환 값</returns> public object ConvertBack(object value, Type targetType, object parameter, CultureInfo cultureInfo) { return System.Convert.ToInt32(value); } #endregion } } |
▶ ViewModel.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 |
using System.Collections.Generic; using System.ComponentModel; namespace TestProject { /// <summary> /// 뷰 모델 /// </summary> public class ViewModel : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 - PropertyChanged /// <summary> /// 속성 변경시 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 제품 리스트 /// </summary> private List<Product> productList; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제품 리스트 - ProductList /// <summary> /// 제품 리스트 /// </summary> public List<Product> ProductList { get { return this.productList; } set { if(value == this.productList) { return; } this.productList = value; NotifyPropertyChanged("ProductList"); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - ViewModel() /// <summary> /// 생성자 /// </summary> public ViewModel() { ProductList = CreateProductList(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 제품 리스트 생성하기 - CreateProductList() /// <summary> /// 제품 리스트 생성하기 /// </summary> /// <returns>제품 리스트</returns> private List<Product> CreateProductList() { List<Product> productList = new List<Product>(); productList.Add(new Product() { ProductName = "Chai" , UnitPrice = 18 , OrderUnit = 10 }); productList.Add(new Product() { ProductName = "Ipoh Coffee" , UnitPrice = 36.8, OrderUnit = 12 }); productList.Add(new Product() { ProductName = "Outback Lager" , UnitPrice = 12 , OrderUnit = 25 }); productList.Add(new Product() { ProductName = "Boston Crab Meat", UnitPrice = 18.4, OrderUnit = 18 }); productList.Add(new Product() { ProductName = "Konbu" , UnitPrice = 6 , OrderUnit = 24 }); return productList; } #endregion #region 속성 변경시 처리하기 - NotifyPropertyChanged(propertyName) /// <summary> /// 속성 변경시 처리하기 /// </summary> /// <param name="propertyName">속성명</param> private void NotifyPropertyChanged(string propertyName) { PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } #endregion } } |