C#/WPF

[WPF] UserControl 에서 Property 속성 추가 방법

기은P 2020. 6. 10. 10:50
반응형

[WPF] UserControl 에서 Property 속성 추가 방법

 

컴포넌트 + 컴포넌트로 사용자 정의 컴포넌트를 생성하고 싶을때 UserControl이라는 Xaml을 생성해서 사용합니다.

일반적인 예시로 색상이 있는 TextBlock과 검은색으로 지정된 TextBlock이라는 컴포넌트를 생성했습니다.

 

 

1. UserControl의 Xaml 코드

 

<UserControl x:Class="EPCM.HMI.Market.Component.ST1Canvas"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:local="clr-namespace:EPCM.HMI.Market.Component"
             mc:Ignorable="d" Background="White" Width="45.167" Height="20.167">
    <Grid>
        <TextBlock x:Name="Title" HorizontalAlignment="Left" Height="8.941" Margin="2.5,1.839,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="35.926" FontSize="6" Text="ST1 - TR" FontWeight="Bold"/>
        <TextBlock x:Name="Voltage" HorizontalAlignment="Left" Height="8.024" Margin="2.5,10,0,0" TextWrapping="Wrap" VerticalAlignment="Top" Width="36.801" FontSize="6" Text="2,000 kVA" Foreground="#FF00B0F0" FontWeight="Bold"/>
    </Grid>
</UserControl>

 

ST1Canvas.xaml이라는 이름으로 UserControl을 생성했고요,

위 자멜 코드와 같이 TextBlock에 x:Name 태그로 임의의 이름을 지정해줍니다.

 

 

 

2. ST1Canvas.cs의 코드

 

namespace EPCM.HMI.Market.Component
{
    /// <summary>
    /// ST1Canvas.xaml에 대한 상호 작용 논리
    /// </summary>
    public partial class ST1Canvas : UserControl
    {
        public ST1Canvas()
        {
            InitializeComponent();
        }

        [Category("TextTitle"), Description("지정할 텍스트")]
        public string TextTitle
        {
            get
            {
                return this.Title.Text;
            }
            set
            {
                this.Title.Text = value;
            }
        }
    }
}

 

이제 Property를 등록할 차례인데, 이 TextTitle이라는 프로퍼티를 등록해서 UserControl을 사용할 때 Title이라는 이름이 변경되게끔 합니다.

TextTitle이라는 String Get, Set 변수를 등록하고 x:Name태그에서 사용한 Title에 초기화를 해줍니다.

 

 

3. UserControl의 사용

 

<UserControl
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
             xmlns:telerik="http://schemas.telerik.com/2008/xaml/presentation"      
             xmlns:global="clr-namespace:EPCM.ENG.Common;assembly=EPCM.ENG.Common"
             xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"  
             xmlns:res="clr-namespace:EPCM.Data.Properties;assembly=EPCM.Data"
             xmlns:UC="clr-namespace:EPCM.HMI.Market.Component"
             xmlns:ed="http://schemas.microsoft.com/expression/2010/drawing" x:Class="EPCM.HMI.Market.View.GridView1"
             mc:Ignorable="d" d:DesignHeight="600" d:DesignWidth="800" Width="3202" Height="2400">

 

clr-namespace:EPCM.HMI.Market.Component

먼저 UC라는 이름의 태그로 namespace를 등록해주고,

 

<UC:ST1Canvas Canvas.Left="86.787" Canvas.Top="97.27" TextTitle="Sample"/>

 

UserControl를 사용하고 TextTitle의 프로퍼티를 호출해서 값을 변경하면 유저컨트롤에서 사용했던 텍스트 값이 자동적으로 변경됩니다.

반응형