■ ResourceDictionary 클래스의 MergedDictionaries 속성에서 Application 객체의 리소스를 사용하는 방법을 보여준다.
▶ ResourceDictionary1.xaml
1 2 3 4 5 6 7 8 9 10 11 12 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <LinearGradientBrush x:Key="LinearGradientBrushKey"> <LinearGradientBrush.GradientStops> <GradientStop Color="Pink" Offset="0" /> <GradientStop Color="Aqua" Offset="1" /> </LinearGradientBrush.GradientStops> </LinearGradientBrush> </ResourceDictionary> |
▶ ResourceDictionary2.xaml
1 2 3 4 5 6 7 8 9 10 11 12 |
<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"> <RadialGradientBrush x:Key="RadialGradientBrushKey"> <RadialGradientBrush.GradientStops> <GradientStop Color="Pink" Offset="0" /> <GradientStop Color="Aqua" Offset="1" /> </RadialGradientBrush.GradientStops> </RadialGradientBrush> </ResourceDictionary> |
▶ MainApplication.xaml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
<Application x:Class="TestProject.MainApplication" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" StartupUri="MainWindow.xaml"> <Application.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries> <ResourceDictionary Source="ResourceDictionary1.xaml" /> <ResourceDictionary Source="ResourceDictionary2.xaml" /> </ResourceDictionary.MergedDictionaries> </ResourceDictionary> </Application.Resources> </Application> |
▶ MainWindow.xaml
1 2 3 4 5 6 7 8 9 10 11 12 |
<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 클래스 : MergedDictionaries 속성을 사용해 Application 객체의 병합 리소스 사용하기" FontFamily="나눔고딕코딩" FontSize="16"> <Grid Name="grid" /> </Window> |
▶ MainWindow.xaml.cs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 |
using System.Windows; using System.Windows.Media; namespace TestProject { /// <summary> /// 메인 윈도우 /// </summary> public partial class MainWindow : Window { //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor ////////////////////////////////////////////////////////////////////////////////////////// Public #region 생성자 - MainWindow() /// <summary> /// 생성자 /// </summary> public MainWindow() { InitializeComponent(); ResourceDictionary resourceDictionary = Application.Current.Resources.MergedDictionaries[0]; LinearGradientBrush brush = resourceDictionary["LinearGradientBrushKey"] as LinearGradientBrush; this.grid.Background = brush; } #endregion } } |