첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
유용한 소스 코드가 있으면 icodebroker@naver.com으로 보내주시면 감사합니다.
블로그 자료는 자유롭게 사용하세요.

■ ListView 클래스 : 의존 속성 리스트 뷰 만들기

----------------------------------------------------------------------------------------------------

DependencyPropertyListView.cs

 

 

using System;

using System.Collections.Generic;

using System.Reflection;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

 

/// <summary>

/// 의존 속성 리스트 뷰

/// </summary>

public class DependencyPropertyListView : ListView

{

    //////////////////////////////////////////////////////////////////////////////////////////////////// Field

    ////////////////////////////////////////////////////////////////////////////////////////// Static

    //////////////////////////////////////////////////////////////////////////////// Public

 

    #region Field

 

    /// <summary>

    /// 타입 속성

    /// </summary>

    public static DependencyProperty TypeProperty;

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Property

    ////////////////////////////////////////////////////////////////////////////////////////// Public

 

    #region 타입 - Type

 

    /// <summary>

    /// 타입

    /// </summary>

    public Type Type

    {

        set

        {

            SetValue(TypeProperty, value);

        }

        get

        {

            return (Type)GetValue(TypeProperty);

        }

    }

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor

    ////////////////////////////////////////////////////////////////////////////////////////// Static

 

    #region 생성자 - DependencyPropertyListView()

 

    /// <summary>

    /// 생성자

    /// </summary>

    static DependencyPropertyListView()

    {

        TypeProperty = DependencyProperty.Register

        (

            "Type",

            typeof(Type),

            typeof(DependencyPropertyListView),

            new PropertyMetadata(null, new PropertyChangedCallback(type_PropertyChangedCallback))

        );

    }

 

    #endregion

 

    ////////////////////////////////////////////////////////////////////////////////////////// Instance

    //////////////////////////////////////////////////////////////////////////////// Public

 

    #region 생성자 - DependencyPropertyListView()

 

    /// <summary>

    /// 생성자

    /// </summary>

    public DependencyPropertyListView()

    {

        GridView gridView = new GridView();

 

        this.View = gridView;

 

 

        GridViewColumn gridViewColumn = new GridViewColumn();

 

        gridViewColumn.Header               = "Name";

        gridViewColumn.Width                = 150;

        gridViewColumn.DisplayMemberBinding = new Binding("Name");

 

        gridView.Columns.Add(gridViewColumn);

 

 

        gridViewColumn = new GridViewColumn();

 

        gridViewColumn.Header = "Owner";

        gridViewColumn.Width  = 100;

 

        gridView.Columns.Add(gridViewColumn);

 

 

        DataTemplate dataTemplate = new DataTemplate();

 

        gridViewColumn.CellTemplate = dataTemplate;

 

        FrameworkElementFactory textBlockFrameworkElementFactory = new FrameworkElementFactory(typeof(TextBlock));

 

        dataTemplate.VisualTree = textBlockFrameworkElementFactory;

 

        Binding binding = new Binding("OwnerType");

 

        binding.Converter = new TypeToTypeNameValueConverter();

 

        textBlockFrameworkElementFactory.SetBinding(TextBlock.TextProperty, binding);

 

 

        gridViewColumn = new GridViewColumn();

 

        gridViewColumn.Header = "Type";

        gridViewColumn.Width  = 100;

 

        gridView.Columns.Add(gridViewColumn);

 

 

        dataTemplate = new DataTemplate();

 

        gridViewColumn.CellTemplate = dataTemplate;

 

        textBlockFrameworkElementFactory = new FrameworkElementFactory(typeof(TextBlock));

 

        dataTemplate.VisualTree = textBlockFrameworkElementFactory;

 

        binding = new Binding("PropertyType");

 

        binding.Converter = new TypeToTypeNameValueConverter();

 

        textBlockFrameworkElementFactory.SetBinding(TextBlock.TextProperty, binding);

 

 

        gridViewColumn = new GridViewColumn();

 

        gridViewColumn.Header               = "Default";

        gridViewColumn.Width                = 75;

        gridViewColumn.DisplayMemberBinding = new Binding("DefaultMetadata.DefaultValue");

 

        gridView.Columns.Add(gridViewColumn);

 

 

        gridViewColumn = new GridViewColumn();

 

        gridViewColumn.Header               = "Read-Only";

        gridViewColumn.Width                = 75;

        gridViewColumn.DisplayMemberBinding = new Binding("DefaultMetadata.IsReadOnly");

 

        gridView.Columns.Add(gridViewColumn);

 

 

        gridViewColumn = new GridViewColumn();

 

        gridViewColumn.Header               = "Usage";

        gridViewColumn.Width                = 75;

        gridViewColumn.DisplayMemberBinding = new Binding("DefaultMetadata.AttachedPropertyUsage");

 

        gridView.Columns.Add(gridViewColumn);

 

 

        gridViewColumn = new GridViewColumn();

 

        gridViewColumn.Header = "Flags";

        gridViewColumn.Width  = 250;

 

        gridView.Columns.Add(gridViewColumn);

 

 

        dataTemplate = new DataTemplate();

 

        gridViewColumn.CellTemplate = dataTemplate;

 

        textBlockFrameworkElementFactory = new FrameworkElementFactory(typeof(TextBlock));

 

        dataTemplate.VisualTree = textBlockFrameworkElementFactory;

 

        binding = new Binding("DefaultMetadata");

 

        binding.Converter = new FrameworkPropertyMetadataToFrameworkPropertyMetadataOptionsValueConverter();

 

        textBlockFrameworkElementFactory.SetBinding(TextBlock.TextProperty, binding);

    }

 

    #endregion

 

    //////////////////////////////////////////////////////////////////////////////////////////////////// Method

    ////////////////////////////////////////////////////////////////////////////////////////// Static

    //////////////////////////////////////////////////////////////////////////////// private

 

    #region 타입 속성 변경시 콜백 처리하기 - type_PropertyChangedCallback(dependencyObject, e)

 

    /// <summary>

    /// 타입 속성 변경시 콜백 처리하기

    /// </summary>

    /// <param name="dependencyObject">DependencyObject</param>

    /// <param name="e">이벤트 인자</param>

    private static void type_PropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs e)

    {

        DependencyPropertyListView dependencyPropertyListView = dependencyObject as DependencyPropertyListView;

 

        Type type = e.NewValue as Type;

 

        dependencyPropertyListView.ItemsSource = null;

 

        if(type != null)

        {

            SortedList<string, DependencyProperty> list = new SortedList<string, DependencyProperty>();

 

            FieldInfo[] fieldInfoArray = type.GetFields();

 

            foreach(FieldInfo fieldInfo in fieldInfoArray)

            {

                if(fieldInfo.FieldType == typeof(DependencyProperty))

                {

                    list.Add(fieldInfo.Name, (DependencyProperty)fieldInfo.GetValue(null));

                }

            }

 

            dependencyPropertyListView.ItemsSource = list.Values;

        }

    }

 

    #endregion

}

 

----------------------------------------------------------------------------------------------------

※ TypeToTypeNameValueConverter : "IValueConverter 인터페이스 : 타입→타입 명칭 값 변환자 만들기" 참조

※ FrameworkPropertyMetadataToFrameworkPropertyMetadataOptionsValueConverter : "IValueConverter 인터페이스 : FrameworkPropertyMetadata → FrameworkPropertyMetadataOptions 값 변환자 만들기" 참조

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요