■ GridControl 클래스에서 다른 모달 윈도우를 사용해 신규 행을 추가하는 방법을 보여준다.
▶ CreateNewRowWindow.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 |
<Window x:Class="TestProject.CreateNewRowWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" SizeToContent="WidthAndHeight" Title="Create New Row" FontFamily="나눔고딕코딩" FontSize="16"> <Window.Resources> <Style TargetType="TextBlock"> <Setter Property="VerticalAlignment" Value="Center" /> </Style> <Style TargetType="TextBox"> <Setter Property="VerticalAlignment" Value="Center" /> <Setter Property="Margin" Value="0 3 0 3"/> </Style> </Window.Resources> <Grid Margin="10"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <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" Text="Text1 : " /> <TextBox x:Name="Text1" Grid.Row="0" Grid.Column="1" MinWidth="100" /> <TextBlock Text="Text2 : " Grid.Row="1" Grid.Column="0" /> <TextBox x:Name="Text2" Grid.Row="1" Grid.Column="1" MinWidth="100" /> <TextBlock Grid.Row="2" Grid.Column="0" Text="Text3 : " /> <TextBox x:Name="Text3" Grid.Row="2" Grid.Column="1" MinWidth="100" /> <StackPanel VerticalAlignment="Bottom" HorizontalAlignment="Right" Orientation="Horizontal" Grid.Row="3" Grid.Column="1"> <Button Margin="5" Padding="3" IsDefault="True" Content="OK" Click="button_Click" /> <Button Margin="5" Padding="3" IsCancel="True" Content="Cancel" /> </StackPanel> </Grid> </Window> |
▶ CreateNewRowWindow.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 |
using System.Windows; namespace TestProject { /// <summary> /// 신규 행 생성 윈도우 /// </summary> public partial class CreateNewRowWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - CreateNewRowWindow() /// <summary> /// 생성자 /// </summary> public CreateNewRowWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 버튼 클릭시 처리하기 - button_Click(sender, e) /// <summary> /// 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void button_Click(object sender, RoutedEventArgs e) { DialogResult = true; Close(); } #endregion } } |
▶ MainViewModel.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 |
using System; using System.ComponentModel; namespace TestProject { /// <summary> /// 메인 뷰 모델 /// </summary> public class MainViewModel : INotifyPropertyChanged { //////////////////////////////////////////////////////////////////////////////////////////////////// Event ////////////////////////////////////////////////////////////////////////////////////////// Public #region 속성 변경시 - PropertyChanged /// <summary> /// 속성 변경시 /// </summary> public event PropertyChangedEventHandler PropertyChanged; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 샘플 바인딩 리스트 /// </summary> private BindingList<Sample> sampleBindingList; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 샘플 바인딩 리스트 - SampleBindingList /// <summary> /// 샘플 바인딩 리스트 /// </summary> public BindingList<Sample> SampleBindingList { get { return this.sampleBindingList; } set { if(value == this.sampleBindingList) { return; } this.sampleBindingList = value; OnPropertyChanged("SampleBindingList"); } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainViewModel() /// <summary> /// 생성자 /// </summary> public MainViewModel() { SampleBindingList = CreateSampleBindingList(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Public #region 신규 행 추가하기 - AddNewRow(text1, text2, text3) /// <summary> /// 신규 행 추가하기 /// </summary> /// <param name="text1">텍스트 1</param> /// <param name="text2">텍스트 2</param> /// <param name="text3">텍스트 3</param> public void AddNewRow(string text1, string text2, string text3) { SampleBindingList.Add(CreateSample(text1, text2, text3)); } #endregion ////////////////////////////////////////////////////////////////////////////////////////// Private #region 샘플 바인딩 리스트 생성하기 - CreateSampleBindingList() /// <summary> /// 샘플 바인딩 리스트 생성하기 /// </summary> /// <returns>샘플 바인딩 리스트</returns> private BindingList<Sample> CreateSampleBindingList() { BindingList<Sample> bindingList = new BindingList<Sample>(); for(int i = 0; i < 5; i++) { bindingList.Add(CreateSample(Guid.NewGuid().ToString(), "text2 " + i, "text3 " + i)); } return bindingList; } #endregion #region 샘플 생성하기 - CreateSample(text1, text2, text3) /// <summary> /// 샘플 생성하기 /// </summary> /// <param name="text1">텍스트 1</param> /// <param name="text2">텍스트 2</param> /// <param name="text3">텍스트 3</param> /// <returns>샘플</returns> private Sample CreateSample(string text1, string text2, string text3) { return new Sample() { Text1 = text1, Text2 = text2, Text3 = text3 }; } #endregion #region 속성 변경시 처리하기 - OnPropertyChanged(propertyName) /// <summary> /// 속성 변경시 처리하기 /// </summary> /// <param name="propertyName">속성명</param> private void OnPropertyChanged(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 |
<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.DataContext> <local:MainViewModel /> </Window.DataContext> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition /> </Grid.RowDefinitions> <Button x:Name="addNewRowButton" Margin="0 5 0 5" Focusable="False" Content="Add new row..." Click="addNewRowButton_Click" /> <dxg:GridControl x:Name="gridControl" Grid.Row="1" ItemsSource="{Binding SampleBindingList}" AutoGenerateColumns="AddNew"> <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 |
using System.Windows; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Property ////////////////////////////////////////////////////////////////////////////////////////// Public #region 메인 뷰 모델 - MainViewModel /// <summary> /// 메인 뷰 모델 /// </summary> public MainViewModel MainViewModel { get { return DataContext as MainViewModel; } } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region Add New Row 버튼 클릭시 처리하기 - addNewRowButton_Click(sender, e) /// <summary> /// Add New Row 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addNewRowButton_Click(object sender, RoutedEventArgs e) { CreateNewRowWindow createNewRowWindow = new CreateNewRowWindow(); createNewRowWindow.ShowDialog(); if(createNewRowWindow.DialogResult.Value == true) { MainViewModel.AddNewRow ( createNewRowWindow.Text1.Text, createNewRowWindow.Text2.Text, createNewRowWindow.Text3.Text ); this.tableView.MoveLastRow(); this.gridControl.Focus(); } } #endregion } } |