728x90
728x170
▶ BlueColor.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="NormalBackgroundSolidColorBrushKey" Color="Blue" />
</ResourceDictionary>
728x90
▶ BlueSize.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<CornerRadius x:Key="NormalCornerRadiusKey">0</CornerRadius>
</ResourceDictionary>
▶ RedColor.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<SolidColorBrush x:Key="NormalBackgroundSolidColorBrushKey" Color="Red" />
</ResourceDictionary>
300x250
▶ RedSize.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<CornerRadius x:Key="NormalCornerRadiusKey">10</CornerRadius>
</ResourceDictionary>
▶ BorderStyle.xaml
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
<Style x:Key="BorderStyleKey" TargetType="Border" >
<Setter Property="Background" Value="{StaticResource NormalBackgroundSolidColorBrushKey}" />
<Setter Property="CornerRadius" Value="{StaticResource NormalCornerRadiusKey}" />
</Style>
</ResourceDictionary>
▶ Skin.cs
namespace TestProject
{
/// <summary>
/// 스킨
/// </summary>
public enum Skin
{
/// <summary>
/// 적색
/// </summary>
Red,
/// <summary>
/// 청색
/// </summary>
Blue
}
}
▶ SkinResourceDictionary.cs
using System;
using System.Windows;
namespace TestProject
{
/// <summary>
/// 스킨 리소스 딕셔너리
/// </summary>
public class SkinResourceDictionary : ResourceDictionary
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Field
////////////////////////////////////////////////////////////////////////////////////////// Private
#region Field
/// <summary>
/// 적색 스킨 URI
/// </summary>
private Uri redSkinURI;
/// <summary>
/// 청색 스킨 URI
/// </summary>
private Uri blueSkinURI;
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Public
#region 적색 스킨 URI - RedSkinURI
/// <summary>
/// 적색 스킨 URI
/// </summary>
public Uri RedSkinURI
{
get
{
return this.redSkinURI;
}
set
{
this.redSkinURI = value;
UpdateSource();
}
}
#endregion
#region 청색 스킨 URI - BlueSkinURI
/// <summary>
/// 청색 스킨 URI
/// </summary>
public Uri BlueSkinURI
{
get
{
return this.blueSkinURI;
}
set
{
this.blueSkinURI = value;
UpdateSource();
}
}
#endregion
//////////////////////////////////////////////////////////////////////////////////////////////////// Method
////////////////////////////////////////////////////////////////////////////////////////// Private
#region 소스 업데이트하기 - UpdateSource()
/// <summary>
/// 소스 업데이트하기
/// </summary>
private void UpdateSource()
{
Uri uri = MainApplication.Skin == Skin.Red ? RedSkinURI : BlueSkinURI;
if(uri != null && base.Source != uri)
{
base.Source = uri;
}
}
#endregion
}
}
▶ MainApplication.xaml
<Application x:Class="TestProject.MainApplication"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:TestProject"
StartupUri="MainWindow.xaml">
<Application.Resources>
<ResourceDictionary>
<ResourceDictionary.MergedDictionaries>
<local:SkinResourceDictionary
RedSkinURI="RESOURCE/RedColor.xaml"
BlueSkinURI="RESOURCE/BlueColor.xaml" />
<local:SkinResourceDictionary
RedSkinURI="RESOURCE/RedSize.xaml"
BlueSkinURI="RESOURCE/BlueSize.xaml" />
<ResourceDictionary Source="RESOURCE/BorderStyle.xaml"/>
</ResourceDictionary.MergedDictionaries>
</ResourceDictionary>
</Application.Resources>
</Application>
▶ MainApplication.xaml.cs
using System.Windows;
namespace TestProject
{
/// <summary>
/// 메인 애플리케이션
/// </summary>
public partial class MainApplication : Application
{
//////////////////////////////////////////////////////////////////////////////////////////////////// Property
////////////////////////////////////////////////////////////////////////////////////////// Static
//////////////////////////////////////////////////////////////////////////////// Public
#region 스킨 - Skin
/// <summary>
/// 스킨
/// </summary>
public static Skin Skin { get; set; } = Skin.Red;
#endregion
}
}
▶ MainWindow.xaml
<Window x:Class="TestProject.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Width="800"
Height="600"
Title="ResourceDictionary 클래스 : 컴파일 및 정적 스키닝 사용하기"
FontFamily="나눔고딕코딩"
FontSize="16">
<Grid>
<Border
Style="{StaticResource BorderStyleKey}"
Width="300"
Height="300" />
</Grid>
</Window>
728x90
그리드형(광고전용)
'C# > WPF' 카테고리의 다른 글
[C#/WPF] FrameworkElement 클래스 : GDI+를 사용해 텍스트 블럭 만들기 (0) | 2022.01.09 |
---|---|
[C#/WPF] x:ClassModifier 속성 사용하기 (0) | 2022.01.09 |
[C#/WPF] ResourceDictionary 클래스 : 느슨한 XAML 스키닝 사용하기 (0) | 2022.01.09 |
[C#/WPF] ResourceDictionary 클래스 : 컴파일 및 동적 스키닝 사용하기 (기능 개선) (0) | 2022.01.09 |
[C#/WPF] ResourceDictionary 클래스 : 컴파일 및 동적 스키닝 사용하기 (0) | 2022.01.09 |
[C#/WPF] DoubleAnimation 클래스 : 화면을 반짝이는 애니메이션 사용하기 (0) | 2022.01.09 |
[C#/WPF] TextBox 클래스 : KeyDown 이벤트를 사용해 ENTER 키를 누르는 경우 포커스 해제하기 (0) | 2022.01.08 |
[C#/WPF] 마이크로폰 볼륨 설정하기 (0) | 2022.01.08 |
[C#/WPF] 정렬과 필터링 가능한 데이터 가상화 사용하기 (0) | 2022.01.06 |
[C#/WPF] Page 엘리먼트 : WindowWidth/WindowHeight/WindowTitle 속성 사용하기 (0) | 2022.01.04 |