C# WPF 리소스 딕셔너리 사용 가이드

1. Window.Resources에서 리소스 선언하기

참고:这种方式으로 선언된 리소스는 현재 Window 내에서만 사용할 수 있습니다.

XAML에서 직접 사용:

<Window x:Class="WpfApplication.DemoWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="데모 윈도우" Height="250" Width="320">
    <Window.Resources>
        <ResourceDictionary>
            <Style TargetType="TextBlock">
                <Setter Property="FontStyle" Value="Italic"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="Blue"/>
            </Style>
            <Style x:Key="customTextStyle" TargetType="TextBlock">
                <Setter Property="Foreground" Value="Purple"/>
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    <StackPanel>
        <TextBlock Text="안녕하세요"/>
        <TextBlock Text="안녕하세요" Style="{StaticResource customTextStyle}"/>
        <TextBlock Text="안녕하세요" Style="{x:Null}"/>
    </StackPanel>
</Window>

코드 비하인드(C#)에서 사용:

<Window x:Class="WpfApplication.DemoWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="데모 윈도우" Height="250" Width="320">
    <Window.Resources>
        <ResourceDictionary>
            <Style TargetType="TextBlock" x:Key="CustomStyle">
                <Setter Property="FontStyle" Value="Italic"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="Blue"/>
            </Style>
        </ResourceDictionary>
    </Window.Resources>
    <StackPanel>
        <TextBlock x:Name="txtElement1" Text="안녕하세요"/>
        <TextBlock x:Name="txtElement2" Text="안녕하세요"/>
        <TextBlock x:Name="txtElement3" Text="안녕하세요"/>
    </StackPanel>
</Window>
public partial class DemoWindow : Window
{
    public DemoWindow()
    {
        InitializeComponent();
        // FindResource 메서드로 리소스 검색
        Style style = (Style)this.FindResource("CustomStyle");
        
        // 정확한 위치를 알 경우 직접 접근 가능
        // Style style = (Style)this.Resources["CustomStyle"];
        
        txtElement1.Style = style;
    }
}

2. App.xaml에서 리소스 선언하기 (전역 사용)

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication"
             x:Class="WpfApplication.App"
             StartupUri="DemoWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <Style TargetType="TextBlock">
                <Setter Property="FontStyle" Value="Italic"/>
                <Setter Property="FontWeight" Value="Bold"/>
                <Setter Property="Foreground" Value="Blue"/>
            </Style>
            <Style x:Key="customTextStyle" TargetType="TextBlock">
                <Setter Property="Foreground" Value="Purple"/>
            </Style>
        </ResourceDictionary>
    </Application.Resources>
</Application>
<Window x:Class="WpfApplication.DemoWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="데모 윈도우" Height="250" Width="320">
    <StackPanel>
        <TextBlock Text="안녕하세요"/>
        <TextBlock Text="안녕하세요" Style="{StaticResource customTextStyle}"/>
        <TextBlock Text="안녕하세요"/>
    </StackPanel>
</Window>

3. 리소스 딕셔너리 파일에서 선언하기

프로젝트에 리소스 딕셔너리 파일을 추가하려면:

  1. 프로젝트에 새 폴더 생성 (예: Resources)
  2. 마우스 우클릭 → 추가 → 리소스 딕셔너리 (WPF)
  3. 리소스를 정의

리소스 딕셔너리 파일 예시 (TextBlockStyles.xaml):

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="TextBlock">
        <Setter Property="FontStyle" Value="Italic"/>
        <Setter Property="FontWeight" Value="Bold"/>
        <Setter Property="Foreground" Value="Blue"/>
    </Style>
    <Style x:Key="customTextStyle" TargetType="TextBlock">
        <Setter Property="Foreground" Value="Purple"/>
    </Style>
</ResourceDictionary>

App.xaml에서 리소스 딕셔너리 병합:

<Application xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:WpfApplication"
             x:Class="WpfApplication.App"
             StartupUri="DemoWindow.xaml">
    <Application.Resources>
        <ResourceDictionary>
            <ResourceDictionary.MergedDictionaries>
                <ResourceDictionary Source="Resources/TextBlockStyles.xaml"/>
                <!--또는-->
                <ResourceDictionary Source="/WpfApplication;component/Resources/TextBlockStyles.xaml"/>
            </ResourceDictionary.MergedDictionaries>
        </ResourceDictionary>
    </Application.Resources>
</Application>

가장 간단한 방법은 파일 경로만 지정하는 것입니다:

<ResourceDictionary Source="Resources/TextBlockStyles.xaml"/>

MainWindow.xaml:

<Window x:Class="WpfApplication.DemoWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="데모 윈도우" Height="250" Width="320">
    <StackPanel>
        <TextBlock Text="안녕하세요"/>
        <TextBlock Text="안녕하세요" Style="{StaticResource customTextStyle}"/>
        <TextBlock Text="안녕하세요"/>
    </StackPanel>
</Window>

4. 스타일 상속 (BasedOn)

BaseOn 속성을 사용하면 기존 스타일을 상속받아 확장할 수 있습니다.

리소스 딕셔너리에서 스타일 상속:

<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
                    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Style TargetType="TextBlock">
        <Setter Property="FontStyle" Value="Italic"/>
        <Setter Property="FontWeight" Value="Bold"/>
    </Style>
    <Style x:Key="greenTextStyle" TargetType="TextBlock" 
           BasedOn="{StaticResource {x:Type TextBlock}}">
        <Setter Property="Foreground" Value="Green"/>
    </Style>
    <Style x:Key="orangeTextStyle" TargetType="TextBlock">
        <Setter Property="Foreground" Value="Orange"/>
    </Style>
</ResourceDictionary>
<Window x:Class="WpfApplication.DemoWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Title="데모 윈도우" Height="250" Width="320">
    <StackPanel>
        <TextBlock Text="안녕하세요"/>
        <TextBlock Text="안녕하세요" Style="{StaticResource greenTextStyle}"/>
        <TextBlock Text="안녕하세요" Style="{StaticResource orangeTextStyle}"/>
    </StackPanel>
</Window>

키가 있는 리소스의 경우 키를 사용하여 상속받을 수도 있습니다:

<Style x:Key="inheritedStyle" TargetType="TextBlock" 
       BasedOn="{StaticResource customTextStyle}">
    <Setter Property="FontSize" Value="20"/>
</Style>

TargetType 지정 방식

TargetType은 직접 타입명을 작성하거나 x:Type 태그를 사용하여 지정할 수 있습니다:

<!-- 직접 타입명 사용 -->
<Style TargetType="TextBlock">

<!-- x:Type 사용 -->
<Style TargetType="{x:Type TextBlock}">

태그: WPF resource-dictionary XAML c-sharp styles

6월 27일 19:48에 게시됨