첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

※ 상기 화면은 Program.cs에서 MainForm2 객체를 실행한 경우이다.

TestProject.zip
다운로드

▶ TestControl1.xaml

<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>

 

728x90

 

▶ TestControl1.xaml.cs

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
    }
}

 

300x250

 

▶ MainForm1.cs

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

<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

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

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
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요