■ 에디터 데이터를 바인딩하는 방법을 보여준다.
▶ 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 |
<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" SizeToContent="WidthAndHeight" Title="에디터 데이터 바인딩 하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <TextBlock Grid.Row="0" Grid.Column="0" VerticalAlignment="Center" Margin="10" Text="Product Name : " /> <dxe:TextEdit x:Name="textEdit" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Margin="10" Width="150" Height="25" EditValue="{Binding ProductName}" /> <TextBlock Grid.Row="1" Grid.Column="0" VerticalAlignment="Center" Margin="10" Text="Unit Price : " /> <dxe:SpinEdit x:Name="spinEdit" Grid.Row="1" Grid.Column="1" VerticalAlignment="Center" Margin="10" Width="150" Height="25" EditValue="{Binding UnitPrice}" /> <TextBlock Grid.Row="2" Grid.Column="0" VerticalAlignment="Center" Margin="10" Text="Category : " /> <dxe:ComboBoxEdit x:Name="comboBoxEdit" Grid.Row="2" Grid.Column="1" Margin="10" Width="150" Height="25" ValueMember="CategoryID" DisplayMember="CategoryName" EditValue="{Binding CategoryID}" IsTextEditable="False" /> <StackPanel Grid.Row="3" Grid.Column="1" Margin="10" Orientation="Horizontal"> <Button x:Name="previousButton" Width="70" Height="25" IsEnabled="False" Content="Previous" Click="previousButton_Click" /> <Button x:Name="nextButton" Margin="5 0 0 0" Width="70" Height="25" Content="Next" Click="nextButton_Click" /> </StackPanel> </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 |
using System.Windows; using TestProject.NorthwindDataSetTableAdapters; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 제품 데이터 테이블 /// </summary> private NorthwindDataSet.ProductsDataTable productsDataTable; /// <summary> /// 현재 레코드 /// </summary> private int currentRecord; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.productsDataTable = new ProductsTableAdapter().GetData(); this.currentRecord = 0; DataContext = this.productsDataTable[currentRecord]; this.comboBoxEdit.ItemsSource = new CategoriesTableAdapter().GetData(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region Previous 버튼 클릭시 처리하기 - previousButton_Click(sender, e) /// <summary> /// Previous 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void previousButton_Click(object sender, RoutedEventArgs e) { this.currentRecord--; UpdateData(); UpdateButtonState(); } #endregion #region Next 버튼 클릭시 처리하기 - nextButton_Click(sender, e) /// <summary> /// Next 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void nextButton_Click(object sender, RoutedEventArgs e) { this.currentRecord++; UpdateData(); UpdateButtonState(); } #endregion //////////////////////////////////////////////////////////////////////////////// Function #region 데이터 갱신하기 - UpdateData() /// <summary> /// 데이터 갱신하기 /// </summary> private void UpdateData() { DataContext = this.productsDataTable[currentRecord]; } #endregion #region 버튼 상태 갱신하기 - UpdateButtonState() /// <summary> /// 버튼 상태 갱신하기 /// </summary> private void UpdateButtonState() { this.previousButton.IsEnabled = this.currentRecord != 0; this.nextButton.IsEnabled = this.currentRecord != this.productsDataTable.Rows.Count - 1; } #endregion } } |