๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
C#

WPF Resource์™€ Style, ControlTemplate ์™„๋ฒฝ ์ •๋ฆฌ

by samie 2025. 4. 6.
WPF Resource์™€ Style, ControlTemplate ์™„๋ฒฝ ์ •๋ฆฌ

๐ŸŽจ 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 ๋“ฑ ๋ฐ์ดํ„ฐ ๋ฐ”์ธ๋”ฉ ์ปจํŠธ๋กค์˜ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• ๋ฐฉ๋ฒ•์„ ๋‹ค๋ฃฐ ์˜ˆ์ •์ž…๋‹ˆ๋‹ค.


๋„์›€์ด ๋˜์…จ๋‹ค๋ฉด ๋Œ“๊ธ€๊ณผ ๊ณต์œ  ๋ถ€ํƒ๋“œ๋ ค์š”! ๋‹ค์Œ ์‹œ๋ฆฌ์ฆˆ์—์„œ ๋งŒ๋‚˜์š”. ๐Ÿ˜Š