첨부 소스 코드는 나눔고딕코딩 폰트를 사용합니다.
728x90
반응형
728x170

TestProject.zip
다운로드

▶ DisplayNameAttribute.cs

using System;

namespace TestProject
{
    /// <summary>
    /// 디스플레이명 어트리뷰트
    /// </summary>
    public class DisplayNameAttribute : Attribute
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 값 - Value

        /// <summary>
        /// 값
        /// </summary>
        public string Value { get; set; }

        #endregion
    }
}

 

728x90

 

▶ Movie.cs

namespace TestProject
{
    /// <summary>
    /// 영화
    /// </summary>
    public class Movie
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 제목 - Title

        /// <summary>
        /// 제목
        /// </summary>
        [DisplayName(Value = "제목")]
        public string Title { get; set; }

        #endregion
        #region 감독 - Director

        /// <summary>
        /// 감독
        /// </summary>
        [DisplayName(Value = "감독")]
        public string Director { get; set; }

        #endregion
        #region 배우 - Actor

        /// <summary>
        /// 배우
        /// </summary>
        [DisplayName(Value = "배우")]
        public string Actor { get; set; }

        #endregion
    }
}

 

300x250

 

▶ HeaderExtension.cs

using System;
using System.Reflection;
using System.Windows.Markup;

namespace TestProject
{
    /// <summary>
    /// 헤더 확장
    /// </summary>
    [MarkupExtensionReturnType(typeof(string))]
    public class HeaderExtension : MarkupExtension
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Property
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 경로 - Path

        /// <summary>
        /// 경로
        /// </summary>
        public string Path { get; set; }

        #endregion
        #region 타입 - Type

        /// <summary>
        /// 타입
        /// </summary>
        public Type Type { get; set; }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - HeaderExtension()

        /// <summary>
        /// 생성자
        /// </summary>
        public HeaderExtension()
        {
        }

        #endregion
        #region 생성자 - HeaderExtension(path)

        /// <summary>
        /// 생성자
        /// </summary>
        /// <param name="path">경로</param>
        public HeaderExtension(string path) : this()
        {
            Path = path;
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 값 제공하기 - ProvideValue(serviceProvider)

        /// <summary>
        /// 값 제공하기
        /// </summary>
        /// <param name="serviceProvider">서비스 공급자</param>
        /// <returns>값</returns>
        public override object ProvideValue(IServiceProvider serviceProvider)
        {
            MemberInfo memberInfo = Type.GetMember(Path)[0];

            return (Attribute.GetCustomAttribute(memberInfo, typeof(DisplayNameAttribute)) as DisplayNameAttribute).Value;
        }

        #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"
    xmlns:local="clr-namespace:TestProject"
    Width="800"
    Height="600"
    Title="MarkupExtension 클래스 : 마크업 확장 사용하기"
    FontFamily="나눔고딕코딩"
    FontSize="16">
    <Grid Margin="10">
        <ListView>
            <ListView.View>
                <GridView>
                    <GridViewColumn
                        Width="300"
                        Header="{local:Header Title, Type=local:Movie}"
                        DisplayMemberBinding="{Binding Title}" />
                    <GridViewColumn
                        Width="200"
                        Header="{local:Header Director, Type=local:Movie}"
                        DisplayMemberBinding="{Binding Director}" />
                    <GridViewColumn
                        Width="400"
                        Header="{local:Header Actor, Type=local:Movie}"
                        DisplayMemberBinding="{Binding Actor}" />
                </GridView>
            </ListView.View>
            <local:Movie
                Title="스타워즈 : 라이즈 오브 스카이워커"
                Director="J.J. 에이브럼스"
                Actor="존 보예가, 데이지 리들리, 아담 드라이버, 오스카 아이삭" />
            <local:Movie
                Title="캡틴 마블"
                Director="애너 보든, 라이언 플렉"
                Actor="브리 라슨, 사무엘 L. 잭슨, 벤 멘델슨, 주드 로" />
            <local:Movie
                Title="베놈"
                Director="루벤 플레셔 "
                Actor="톰 하디, 미셸 윌리엄스" />
        </ListView>
    </Grid>
</Window>
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요