๐จ WPF Resource, Style, ControlTemplate ์๋ฒฝ ๊ฐ์ด๋
WPF์์๋ ๋์์ธ ์์๋ฅผ ํจ์จ์ ์ผ๋ก ๊ด๋ฆฌํ๊ธฐ ์ํด Resource, Style, ControlTemplate์ ์ ๊ณตํฉ๋๋ค.
์ด ๊ธ์์๋ ๊ฐ๊ฐ์ ๊ฐ๋
๊ณผ ์ญํ , ์ฐจ์ด์ , ๊ทธ๋ฆฌ๊ณ ์ค์ต ์์ ๋ฅผ ํตํด ์ค์ ์ ์ฉ ๋ฐฉ๋ฒ๊น์ง ์์๋ด
๋๋ค.
๐ ๋ชฉ์ฐจ
1. Resource๋?
Resource๋ WPF์์ ์ฌ์ฌ์ฉ ๊ฐ๋ฅํ ๊ฐ๋ค์ ์ ์ํ๊ณ ๊ด๋ฆฌํ ์ ์๋ ๊ธฐ๋ฅ์
๋๋ค.
๋ํ์ ์ธ ์๋ก ์์, ๋ธ๋ฌ์, ์ปจํธ๋กค ์คํ์ผ ๋ฑ์ด ์์ต๋๋ค.
<Window.Resources>
<SolidColorBrush x:Key="PrimaryColor" Color="CornflowerBlue"/>
</Window.Resources>
<TextBlock Foreground="{StaticResource PrimaryColor}" Text="๋ฆฌ์์ค ์ ์ฉ!" />
- StaticResource: ์ปดํ์ผ ์์ ์ ๋ฆฌ์์ค๋ฅผ ์ฐธ์กฐ
- DynamicResource: ๋ฐํ์ ์์ ์ ๋ฆฌ์์ค๋ฅผ ์ฐธ์กฐ
2. Style์ด๋?
Style์ ์ปจํธ๋กค์ ์์ฑ์ ์ผ๊ด์ ์ผ๋ก ์ ์ํ์ฌ ์ฌ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ๋๋ค.
โ๏ธ ๊ธฐ๋ณธ ์คํ์ผ ์ ์
<Window.Resources>
<Style x:Key="BasicTextStyle" TargetType="TextBlock">
<Setter Property="FontSize" Value="16"/>
<Setter Property="Foreground" Value="DarkSlateBlue"/>
</Style>
</Window.Resources>
<TextBlock Text="์คํ์ผ ์ ์ฉ" Style="{StaticResource BasicTextStyle}" />
โ๏ธ ์์์ ์คํ์ผ
Key ์์ด TargetType๋ง ์ง์ ํ๋ฉด, ํด๋น ํ์ ์ ์ฒด์ ์๋ ์ ์ฉ๋ฉ๋๋ค.
<Style TargetType="Button">
<Setter Property="Background" Value="LightGreen"/>
</Style>
3. ControlTemplate์ด๋?
ControlTemplate์ ์ปจํธ๋กค์ ์๊ฐ์ ๊ตฌ์กฐ๋ฅผ ์์ ํ ์ฌ์ ์ํ ์ ์๋ ๊ธฐ๋ฅ์ ๋๋ค.
โ๏ธ ๋ฒํผ์ ๋ชจ์์ ๋ฐ๊พธ๋ ํ ํ๋ฆฟ ์์
<Window.Resources>
<ControlTemplate x:Key="CircleButtonTemplate" TargetType="Button">
<Border Background="{TemplateBinding Background}"
CornerRadius="50" BorderBrush="Black" BorderThickness="2">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Window.Resources>
<Button Content="โก" Template="{StaticResource CircleButtonTemplate}" Width="100" Height="100"/>
์ ์์ ๋ ๋ฒํผ์ ์ํ์ผ๋ก ํ์ํ๋ฉด์, ํ
์คํธ๋ฅผ ์ค์์ ๋ฐฐ์นํฉ๋๋ค.
TemplateBinding์ ์๋ ์ปจํธ๋กค์ ์์ฑ ๊ฐ์ ํ
ํ๋ฆฟ์์ ๊ฐ์ ธ์ฌ ๋ ์ฌ์ฉํฉ๋๋ค.
4. ์ค์ ์์ : ์คํ์ผ๊ณผ ํ ํ๋ฆฟ ํ์ฉ
์ ์ฒด ์์ ๋ฅผ ํตํด ์ค์ ๋ก ์ ์ฉ๋ ๋ชจ์ต์ ๋๋ค.
โ๏ธ ์ ์ฒด ์์ XAML
<Window x:Class="StyleTemplateSample.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="์คํ์ผ & ํ
ํ๋ฆฟ" Height="300" Width="400">
<Window.Resources>
<SolidColorBrush x:Key="AccentColor" Color="DarkOrange"/>
<Style TargetType="TextBlock">
<Setter Property="FontWeight" Value="Bold"/>
<Setter Property="Foreground" Value="{StaticResource AccentColor}"/>
</Style>
<ControlTemplate x:Key="RoundedButton" TargetType="Button">
<Border Background="{TemplateBinding Background}"
CornerRadius="20" Padding="10"
BorderBrush="DarkOrange" BorderThickness="2">
<ContentPresenter HorizontalAlignment="Center" VerticalAlignment="Center"/>
</Border>
</ControlTemplate>
</Window.Resources>
<StackPanel Margin="20" VerticalAlignment="Center">
<TextBlock Text="์คํ์ผ๊ณผ ํ
ํ๋ฆฟ ์ ์ฉ ์์ " FontSize="18"/>
<Button Content="๋๋ฌ๋ณด์ธ์" Template="{StaticResource RoundedButton}"
Background="LightSalmon" Margin="10"/>
</StackPanel>
</Window>
5. ๋ง๋ฌด๋ฆฌ ๋ฐ ๋ค์ ๊ธ ์๋ด
์ด ๊ธ์์๋ WPF์์ ๊ฐ์ฅ ๊ธฐ๋ณธ์ ์ด์ง๋ง ๊ฐ๋ ฅํ ๊ธฐ๋ฅ์ธ Resource, Style, ControlTemplate์ ์ฐจ์ด์ ํ์ฉ๋ฒ์ ์ดํด๋ณด์์ต๋๋ค.
- ๐ฏ Resource๋ ๋ชจ๋ ๊ฐ์ ์ฌ์ฌ์ฉ์ ์ํ ํค-๊ฐ ์ ์ฅ์
- ๐ฏ Style์ ์ปจํธ๋กค ์์ฑ ์ ์๋ฅผ ๋ชจ๋ํ
- ๐ฏ ControlTemplate์ UI์ ์ธํ ์์ฒด๋ฅผ ์ปค์คํฐ๋ง์ด์ง
๋ค์ ๊ธ์์๋ DataTemplate์ ํ์ฉํ ListBox, ComboBox ๋ฑ ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ ์ปจํธ๋กค์ ์ปค์คํฐ๋ง์ด์ง ๋ฐฉ๋ฒ์ ๋ค๋ฃฐ ์์ ์ ๋๋ค.
๋์์ด ๋์ จ๋ค๋ฉด ๋๊ธ๊ณผ ๊ณต์ ๋ถํ๋๋ ค์! ๋ค์ ์๋ฆฌ์ฆ์์ ๋ง๋์. ๐
'C#' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
WPF TreeView์ HierarchicalDataTemplate ์์ ์ ๋ณต (0) | 2025.04.08 |
---|---|
WPF DataTemplate๊ณผ ItemsControl ์์ ์ดํด (0) | 2025.04.07 |
WPF UserControl vs CustomControl ์ฐจ์ด์ ์ฌ์ฉ ์์ (0) | 2025.04.05 |
WPF MVVM Command ํจํด ์์ ์ ๋ณต - RelayCommand ๊ตฌํ (0) | 2025.04.04 |
C# WPF ๋ฐ์ดํฐ ๋ฐ์ธ๋ฉ ์ฌ์ธต ๋ถ์ ๋ฐ ๊ณ ๊ธ ํ์ฉ๋ฒ (0) | 2025.04.03 |