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

■ ListBox 클래스 : ItemsPanel 속성을 사용해 색상 그리드 박스 만들기

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

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Controls.Primitives;

using System.Windows.Media;

using System.Windows.Shapes;

 

/// <summary>

/// 색상 그리드 박스

/// </summary>

public class ColorGridBox : ListBox

{

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

    ////////////////////////////////////////////////////////////////////////////////////////// Private

 

    #region Field

 

    /// <summary>

    /// 색상 문자열 배열

    /// </summary>

    private string[] colorStringArray =

    {

        "Black"    , "Brown"       , "DarkGreen"  , "MidnightBlue",

        "Navy"     , "DarkBlue"    , "Indigo"     , "DimGray"     ,

        "DarkRed"  , "OrangeRed"   , "Olive"      , "Green"       ,

        "Teal"     , "Blue"        , "SlateGray"  , "Gray"        ,

        "Red"      , "Orange"      , "YellowGreen", "SeaGreen"    ,

        "Aqua"     , "LightBlue"   , "Violet"     , "DarkGray"    ,

        "Pink"     , "Gold"        , "Yellow"     , "Lime"        ,

        "Turquoise", "SkyBlue"     , "Plum"       , "LightGray"   ,

        "LightPink", "Tan"         , "LightYellow", "LightGreen"  ,

        "LightCyan", "LightSkyBlue", "Lavender"   , "White"

    };

 

    #endregion

 

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

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

 

    #region 생성자 - ColorGridBox()

 

    /// <summary>

    /// 생성자

    /// </summary>

    public ColorGridBox()

    {

        FrameworkElementFactory uniformGridFrameworkElementFactory = new FrameworkElementFactory(typeof(UniformGrid));

 

        uniformGridFrameworkElementFactory.SetValue(UniformGrid.ColumnsProperty, 8);

 

        ItemsPanel = new ItemsPanelTemplate(uniformGridFrameworkElementFactory);

 

        foreach(string colorString in this.colorStringArray)

        {

            Rectangle rectangle = new Rectangle();

 

            rectangle.Width  = 12;

            rectangle.Height = 12;

            rectangle.Margin = new Thickness(4);

            rectangle.Fill   = (Brush)typeof(Brushes).GetProperty(colorString).GetValue(null, null);

 

            Items.Add(rectangle);

 

            ToolTip toolTip = new ToolTip();

 

            toolTip.Content = colorString;

 

            rectangle.ToolTip = toolTip;

        }

 

        SelectedValuePath = "Fill";

    }

 

    #endregion

}

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요