■ WPF 사용자 컨트롤을 사용하는 방법을 보여준다.
※ 상기 화면은 Program.cs에서 MainForm2 객체를 실행한 경우이다.
▶ TestControl1.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 |
<UserControl x:Class="TestProject.TestControl1" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="5" /> <RowDefinition Height="Auto" /> <RowDefinition Height="10" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Label Name="titleLabel" Grid.Row="0" Height="30" Content="WPF 사용자 컨트롤" /> <TextBox Name="textBox" Grid.Row="2" Margin="5 0 5 0" Height="30" /> <Button Name="addButton" Grid.Row="4" Width="120" Height="30" VerticalAlignment="Top" Content=" 항목 추가" Click="addButton_Click" /> </Grid> </UserControl> |
▶ TestControl1.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 |
using System.Windows; using System.Windows.Controls; namespace TestProject { /// <summary> /// 테스트 컨트롤 1 /// </summary> public partial class TestControl1 : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Declaration ////////////////////////////////////////////////////////////////////////////////////////// Public #region Delegate /// <summary> /// 추가 버튼 클릭시 델리게이트 /// </summary> /// <param name="strItem">텍스트</param> public delegate void AddButtonClickedDelegate(string strText); #endregion #region Event /// <summary> /// 추가 버튼 클릭시 /// </summary> public event AddButtonClickedDelegate AddButtonClicked; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - TestControl1() /// <summary> /// 생성자 /// </summary> public TestControl1() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 항목 추가 버튼 클릭시 처리하기 - addButton_Click(sender, e) /// <summary> /// 항목 추가 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void addButton_Click(object sender, RoutedEventArgs e) { AddButtonClicked?.Invoke(this.textBox.Text); } #endregion } } |
▶ MainForm1.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.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 #1 /// </summary> public partial class MainForm1 : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm1() /// <summary> /// 생성자 /// </summary> public MainForm1() { InitializeComponent(); TestControl1 testControl1 = new TestControl1(); this.elementHost.Child = testControl1; testControl1.AddButtonClicked += new TestControl1.AddButtonClickedDelegate(testControl1_AddButtonClicked); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private //////////////////////////////////////////////////////////////////////////////// Event #region 테스트 컨트롤 1 추가 버튼 클릭시 처리하기 - testControl_AddButtonClicked(text) /// <summary> /// 테스트 컨트롤 1 추가 버튼 클릭시 처리하기 /// </summary> /// <param name="text">텍스트</param> private void testControl1_AddButtonClicked(string text) { this.listBox.Items.Add(text); } #endregion } } |
▶ TestControl2.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 |
<UserControl x:Class="TestProject.TestControl2" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Width="300" Height="300" FontFamily="나눔고딕코딩" FontSize="16"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> <RowDefinition Height="5" /> <RowDefinition Height="50*" /> <RowDefinition Height="5" /> <RowDefinition Height="50*" /> </Grid.RowDefinitions> <Label Name="lblTitle" Grid.Row="0" Height="30" Content="WPF 사용자 컨트롤" /> <Button Name="blueButton" Grid.Row="2" Margin="5 0 5 0" Width="150" Height="30" RenderTransformOrigin="0.5 0.5" Background="Blue" Content="파랑색(45도)" Click="blueButton_Click"> <Button.LayoutTransform> <RotateTransform Angle="45" /> </Button.LayoutTransform> </Button> <Button Name="yellowButton" Grid.Row="5" Margin="5 0 5 0" Width="150" Height="30" Background="Yellow" Content="노랑색" Click="yellowButton_Click" /> </Grid> </UserControl> |
▶ TestControl2.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 |
using System.Windows; using System.Windows.Controls; namespace TestProject { /// <summary> /// 테스트 컨트롤 2 /// </summary> public partial class TestControl2 : UserControl { //////////////////////////////////////////////////////////////////////////////////////////////////// Declaration ////////////////////////////////////////////////////////////////////////////////////////// Public #region Delegate /// <summary> /// 각도 버튼 클릭시 델리게이트 /// </summary> /// <param name="nAngle">각도</param> public delegate void AngleButtonClickedDelegate(int nAngle); /// <summary> /// 색상 버튼 클릭시 델리게이트 /// </summary> /// <param name="strColor"></param> public delegate void ColorButtonClickedDelegate(string strColor); #endregion #region Event /// <summary> /// 각도 버튼 클릭시 /// </summary> public event AngleButtonClickedDelegate AngleButtonClicked; /// <summary> /// 색상 버튼 클릭시 /// </summary> public event ColorButtonClickedDelegate ColorButtonClicked; #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - TestControl2() /// <summary> /// 생성자 /// </summary> public TestControl2() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 파랑색 버튼 클릭시 처리하기 - blueButton_Click(sender, e) /// <summary> /// 파랑색 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void blueButton_Click(object sender, RoutedEventArgs e) { ColorButtonClicked(this.blueButton.Background.ToString()); AngleButtonClicked(45); } #endregion #region 노랑색 버튼 클릭시 처리하기 - yellowButton_Click(sender, e) /// <summary> /// 노랑색 버튼 클릭시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void yellowButton_Click(object sender, RoutedEventArgs e) { ColorButtonClicked(this.yellowButton.Background.ToString()); AngleButtonClicked(0); } #endregion } } |
▶ MainForm2.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 |
using System; using System.Windows.Forms; namespace TestProject { /// <summary> /// 메인 폼 #2 /// </summary> public partial class MainForm2 : Form { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainForm2() /// <summary> /// 생성자 /// </summary> public MainForm2() { InitializeComponent(); } #endregion //////////////////////////////////////////////////////////////////////////////////////////////////// Method ////////////////////////////////////////////////////////////////////////////////////////// Private #region 폼 로드시 처리하기 - Form_Load(sender, e) /// <summary> /// 폼 로드시 처리하기 /// </summary> /// <param name="sender">이벤트 발생자</param> /// <param name="e">이벤트 인자</param> private void Form_Load(object sender, EventArgs e) { TestControl2 testControl2 = new TestControl2(); this.elementHost.Child = testControl2; testControl2.AngleButtonClicked += new TestControl2.AngleButtonClickedDelegate(testControl2_AngleButtonClicked); testControl2.ColorButtonClicked += new TestControl2.ColorButtonClickedDelegate(testControl2_ColorButtonClicked); } #endregion #region 테스트 컨트롤 2 각도 버튼 클릭시 처리하기 - testControl2_AngleButtonClicked(angle) /// <summary> /// 테스트 컨트롤 2 각도 버튼 클릭시 처리하기 /// </summary> /// <param name="angle">각도</param> private void testControl2_AngleButtonClicked(int angle) { this.angleTextBox.Text = angle.ToString(); } #endregion #region 테스트 컨트롤 2 색상 버튼 클릭시 처리하기 - testControl2_ColorButtonClicked(strColor) /// <summary> /// 테스트 컨트롤 2 색상 버튼 클릭시 처리하기 /// </summary> /// <param name="color">색상</param> private void testControl2_ColorButtonClicked(string color) { this.colorTextBox.Text = color; } #endregion } } |