■ GridViewColumn 클래스 : CellTemplate 속성 사용하기

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

using System;

using System.Collections.Generic;

using System.Reflection;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

 

/// <summary>

/// 메인 윈도우

/// </summary>

public class MainWindow : Window

{

    [STAThread]

    public static void Main()

    {

        Application app = new Application();

 

        app.Run(new MainWindow());

    }

 

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

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

 

    #region 생성자 - MainWindow()

 

    /// <summary>

    /// 생성자

    /// </summary>

    public MainWindow()

    {

        Title  = "ListView 클래스 사용하기";

        Width  = 600;

        Height = 450;

 

 

        ListView listView = new ListView();

 

        Content = listView;

 

 

        GridView gridView = new GridView();

 

        listView.View = gridView;

 

 

        GridViewColumn nameGridViewColumn = new GridViewColumn();

 

        nameGridViewColumn.Header               = "속성명";

        nameGridViewColumn.Width                = 200;

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

 

        gridView.Columns.Add(nameGridViewColumn);

 

 

        GridViewColumn valueGridViewColumn = new GridViewColumn();

 

        valueGridViewColumn.Header               = "값";

        valueGridViewColumn.Width                = 200;

 

        gridView.Columns.Add(valueGridViewColumn);

 

 

        DataTemplate valueDataTemplate = new DataTemplate(typeof(SystemInformation));

 

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

 

        textBlockFrameworkElementFactory.SetValue(TextBlock.HorizontalAlignmentProperty, HorizontalAlignment.Right);

 

        textBlockFrameworkElementFactory.SetBinding(TextBlock.TextProperty, new Binding("Value"));

 

        valueDataTemplate.VisualTree = textBlockFrameworkElementFactory;

 

        valueGridViewColumn.CellTemplate = valueDataTemplate;

 

 

        PropertyInfo[] propertyInfoArray = typeof(SystemParameters).GetProperties();

 

        SortedList<string, SystemInformation> sortList = new SortedList<string, SystemInformation>();

 

        foreach(PropertyInfo propertyInfo in propertyInfoArray)

        {

            if(propertyInfo.PropertyType != typeof(ResourceKey))

            {

                SystemInformation systemInformation = new SystemInformation();

 

                systemInformation.Name  = propertyInfo.Name;

                systemInformation.Value = propertyInfo.GetValue(null, null);

 

                sortList.Add(propertyInfo.Name, systemInformation);

            }

        }

 

        listView.ItemsSource = sortList.Values;

    }

 

    #endregion

}

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

Posted by 사용자 icodebroker

댓글을 달아 주세요