⚙️ WPF UserControl vs CustomControl 차이 완전 정리
WPF에서 UI를 재사용하거나 독립적인 컴포넌트로 만들고 싶을 때, 흔히 UserControl 또는 CustomControl을 사용하게 됩니다.
하지만 두 컨트롤은 구조와 활용 목적이 다르므로 정확한 차이를 알고 사용하는 것이 중요합니다.
📚 목차
1. 개요 및 정의
WPF는 UI를 모듈화하고 재사용할 수 있는 여러 방식의 사용자 정의 컨트롤을 제공합니다.
그 중 가장 대표적인 것이 UserControl과 CustomControl입니다.
- UserControl: XAML + CodeBehind로 UI와 로직을 구성
- CustomControl: 스타일과 템플릿 중심으로 완전히 새로운 컨트롤을 정의
2. UserControl vs CustomControl 비교
항목 | UserControl | CustomControl |
---|---|---|
기본 구조 | XAML + CodeBehind | 코드 + 스타일(ResourceDictionary) |
UI 재사용성 | 한정적 (복잡한 UI 적합) | 높음 (템플릿 교체 가능) |
스타일/스킨 지원 | 제한적 | 강력 (Themes 지원) |
템플릿 커스터마이징 | 어려움 | 매우 쉬움 |
사용 난이도 | 초급~중급 | 중급~고급 |
3. 언제 어떤 걸 써야 할까?
- ✔️ UserControl이 적합한 경우:
- 여러 컨트롤을 조합해서 하나의 UI 블록을 만들고 싶을 때
- 간단한 재사용 컴포넌트를 빠르게 만들고자 할 때
- ✔️ CustomControl이 적합한 경우:
- 스타일 변경이 빈번하거나 테마 기능이 필요한 경우
- 스킨 교체, 리소스 사전 등을 활용할 때
- 기존 컨트롤의 외형을 바꾸되, 기능은 유지하고 싶을 때
4. 실전 예제: 간단한 UserControl 구현
✔️ Step 1: MyUserControl.xaml 생성
<UserControl x:Class="MyApp.Controls.MyUserControl"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Height="100" Width="300">
<Border BorderBrush="Gray" BorderThickness="1" Padding="10">
<StackPanel>
<TextBlock Text="👋 사용자 정의 컨트롤" FontSize="16"/>
<Button Content="눌러보세요!" Click="Button_Click"/>
</StackPanel>
</Border>
</UserControl>
✔️ MyUserControl.xaml.cs
using System.Windows;
using System.Windows.Controls;
namespace MyApp.Controls
{
public partial class MyUserControl : UserControl
{
public MyUserControl()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
MessageBox.Show("UserControl에서 클릭됨!");
}
}
}
✔️ MainWindow.xaml에서 사용
<Window x:Class="MyApp.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:MyApp.Controls"
Title="UserControl 테스트" Height="200" Width="400">
<Grid>
<controls:MyUserControl />
</Grid>
</Window>
💡 참고: CustomControl은 Control을 상속하고, Themes/Generic.xaml에 스타일을 정의해야 합니다.
5. 마무리 및 다음 주제 예고
UserControl과 CustomControl은 WPF에서 재사용 가능한 컴포넌트를 만들 때 매우 유용합니다.
각각의 특징을 이해하고, 상황에 따라 적절히 선택하면 더 구조적인 애플리케이션 개발이 가능합니다.
다음 시리즈에서는 Style과 Template의 차이점, 그리고 실전 스타일 커스터마이징 예제를 소개합니다.
도움이 되셨다면 댓글과 좋아요 부탁드려요! 다음 글에서 만나요. 😊
'C#' 카테고리의 다른 글
WPF DataTemplate과 ItemsControl 완전 이해 (0) | 2025.04.07 |
---|---|
WPF Resource와 Style, ControlTemplate 완벽 정리 (0) | 2025.04.06 |
WPF MVVM Command 패턴 완전 정복 - RelayCommand 구현 (0) | 2025.04.04 |
C# WPF 데이터 바인딩 심층 분석 및 고급 활용법 (0) | 2025.04.03 |
WPF에서 다국어 지원(Localization) 구현하기 (0) | 2025.04.02 |