■ 데이터 원본을 컨트롤에 바인딩하는 것은 Windows Forms를 사용하든 WPF를 사용하든 사용자에게 기본 데이터에 대한 액세스 권한을 제공하는 데 필수적이다. Windows Forms 및 WPF 컨트롤을 모두 포함하는 하이브리드 애플리케이션에서 데이터 바인딩을 사용하는 방법을 보여준다.
▶ 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 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 |
<Window x:Class="TestProject.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:winform="clr-namespace:System.Windows.Forms;assembly=System.Windows.Forms" Width="800" Height="600" Title="TestProject" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Name="mainGrid" Margin="10"> <Grid.Resources> <DataTemplate x:Key="ListItemTemplateKey"> <TextBlock Text="{Binding Path=ContactName}" /> </DataTemplate> </Grid.Resources> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> <RowDefinition Height="Auto" /> </Grid.RowDefinitions> <Grid.ColumnDefinitions> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> <ColumnDefinition Width="Auto" /> </Grid.ColumnDefinitions> <StackPanel Grid.Row="0" Grid.Column="0" Orientation="Vertical"> <Label Margin="10 10 0 0">List of Customers</Label> <ListBox Name="listBox" HorizontalAlignment="Left" Margin="10 10 10 10" Width="200" Height="200" ItemTemplate="{StaticResource ListItemTemplateKey}" IsSynchronizedWithCurrentItem="True" /> </StackPanel> <StackPanel Grid.Row="0" Grid.Column="1" Orientation="Vertical"> <Label HorizontalAlignment="Right" Margin="10 10 0 0" Height="25"> First Name </Label> <Label HorizontalAlignment="Right" Margin="10 10 0 0" Height="25"> Company Name </Label> <Label HorizontalAlignment="Right" Margin="10 10 0 0" Height="25"> Phone </Label> <Label HorizontalAlignment="Right" Margin="10 10 0 0" Height="25"> Address </Label> <Label HorizontalAlignment="Right" Margin="10 10 0 0" Height="25"> City </Label> <Label HorizontalAlignment="Right" Margin="10 10 0 0" Height="25"> Region </Label> <Label HorizontalAlignment="Right" Margin="10 10 0 0" Height="25"> Postal Code </Label> </StackPanel> <StackPanel Grid.Row="0" Grid.Column="2" Orientation="Vertical"> <TextBox Margin="10 10 0 0" Width="200" Height="25" VerticalContentAlignment="Center" Text="{Binding Path=ContactName}" /> <TextBox Margin="10 10 0 0" Width="200" Height="25" VerticalContentAlignment="Center" Text="{Binding Path=CompanyName}" /> <TextBox Margin="10 10 0 0" Width="200" Height="25" VerticalContentAlignment="Center" Text="{Binding Path=Phone}" /> <TextBox Margin="10 10 0 0" Width="200" Height="25" VerticalContentAlignment="Center" Text="{Binding Path=Address}" /> <TextBox Margin="10 10 0 0" Width="200" Height="25" VerticalContentAlignment="Center" Text="{Binding Path=City}" /> <TextBox HorizontalAlignment="Left" Margin="10 10 0 0" Width="40" Height="25" VerticalContentAlignment="Center" Text="{Binding Path=Region}" /> <TextBox Margin="10 10 0 0" Width="70" Height="25" VerticalContentAlignment="Center" HorizontalAlignment="Left" Text="{Binding Path=PostalCode}" /> </StackPanel> <WindowsFormsHost Grid.Row="1" Grid.Column="0" Grid.ColumnSpan="3" Margin="10 10 0 10" Height="250"> <winform:DataGridView x:Name="dataGridView" /> </WindowsFormsHost> </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 System.Windows.Data; using TestProject.NorthwindDataSetTableAdapters; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Field ////////////////////////////////////////////////////////////////////////////////////////// Private #region Field /// <summary> /// 바인딩 소스 /// </summary> private System.Windows.Forms.BindingSource bindingSource; /// <summary> /// NORTHWIND 데이터셋 /// </summary> private NorthwindDataSet dataSet; /// <summary> /// 고객 테이블 어댑터 /// </summary> private CustomersTableAdapter customersTableAdapter = new CustomersTableAdapter(); /// <summary> /// 주문 테이블 어댑터 /// </summary> private OrdersTableAdapter ordersTableAdapter = new OrdersTableAdapter(); #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); this.dataSet = new NorthwindDataSet(); this.dataSet.DataSetName = "nwDataSet"; this.bindingSource = new System.Windows.Forms.BindingSource(); this.bindingSource.DataMember = "Customers"; this.bindingSource.DataSource = this.dataSet; Loaded += Window_Loaded; } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 윈도우 로드시 처리하기 - Window_Loaded(sender, e) /// <summary> /// 윈도우 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Window_Loaded(object sender, RoutedEventArgs e) { this.customersTableAdapter.ClearBeforeFill = true; this.customersTableAdapter.Fill(this.dataSet.Customers); this.ordersTableAdapter.Fill(this.dataSet.Orders); this.mainGrid.DataContext = this.bindingSource; this.listBox.ItemsSource = this.bindingSource; this.dataGridView.DataSource = this.bindingSource; this.dataGridView.DataMember = "CustomersOrders"; BindingListCollectionView collectionView = CollectionViewSource.GetDefaultView(this.bindingSource) as BindingListCollectionView; collectionView.CurrentChanged += collectionView_CurrentChanged; } #endregion #region 컬렉션 뷰 현재 항목 변경시 처리하기 - collectionView_CurrentChanged(sender, e) /// <summary> /// 컬렉션 뷰 현재 항목 변경시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void collectionView_CurrentChanged(object sender, EventArgs e) { BindingListCollectionView collectionView = sender as BindingListCollectionView; this.bindingSource.Position = collectionView.CurrentPosition; } #endregion } } |