첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ ResourceHelper.cs

using System.Windows;

namespace TestProject
{
    /// <summary>
    /// 리소스 헬퍼
    /// </summary>
    public class ResourceHelper
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 텍스트 블럭 스타일 키 - TextBlockStyleKey

        /// <summary>
        /// 텍스트 블럭 스타일 키
        /// </summary>
        public static ComponentResourceKey TextBlockStyleKey
        {
            get
            {
                return new ComponentResourceKey(typeof(ResourceHelper), nameof(TextBlockStyleKey));
            }
        }

        #endregion
    }
}

 

728x90

 

▶ Generic.xaml

<ResourceDictionary
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="clr-namespace:TestProject">
    <Style x:Key="{ComponentResourceKey TypeInTargetAssembly={x:Type local:ResourceHelper}, ResourceId=TextBlockStyleKey}"
        TargetType="{x:Type TextBlock}">
        <Setter Property="Background" Value="Gold" />
        <Setter Property="FontSize"   Value="64"   />
    </Style>
</ResourceDictionary>

 

300x250

 

▶ 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"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="ComponentResourceKey 클래스 : 제네릭 파일(generic.xaml) 리소스 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="12">
    <Grid>
        <TextBlock
            Style="{DynamicResource {x:Static local:ResourceHelper.TextBlockStyleKey}}"
            HorizontalAlignment="Center"
            VerticalAlignment="Center"
            Text="Hello, World" />
    </Grid>
</Window>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요