첨부 실행 코드는 나눔고딕코딩 폰트를 사용합니다.
본 블로그는 광고를 포함하고 있습니다.
광고 클릭에서 발생하는 수익금은 모두 블로그 콘텐츠 향상을 위해 쓰여집니다.

728x90
반응형
728x170

TestProject.zip
다운로드

▶ ColorGridBox.cs

using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Media;
using System.Windows.Shapes;

namespace TestProject
{
    /// <summary>
    /// 색상 그리드 박스
    /// </summary>
    public class ColorGridBox : ListBox
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Field
        ////////////////////////////////////////////////////////////////////////////////////////// Private

        #region Field

        /// <summary>
        /// 색상명 배열
        /// </summary>
        private string[] colorNameArray =
        {
            "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 colorName in this.colorNameArray)
            {
                Rectangle rectangle = new Rectangle();

                rectangle.Width  = 12;
                rectangle.Height = 12;
                rectangle.Margin = new Thickness(4);
                rectangle.Fill   = (Brush)typeof(Brushes).GetProperty(colorName).GetValue(null, null);

                Items.Add(rectangle);

                ToolTip toolTip = new ToolTip();

                toolTip.Content = colorName;

                rectangle.ToolTip = toolTip;
            }

            SelectedValuePath = "Fill";
        }

        #endregion
    }
}

 

728x90

 

▶ MainWindow.cs

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace TestProject
{
    /// <summary>
    /// 메인 윈도우
    /// </summary>
    public class MainWindow : Window
    {
        //////////////////////////////////////////////////////////////////////////////////////////////////// Constructor
        ////////////////////////////////////////////////////////////////////////////////////////// Public

        #region 생성자 - SelectColorFromGrid()

        /// <summary>
        /// 생성자
        /// </summary>
        public MainWindow()
        {
            Title         = "ListBox 클래스 : ItemsPanel 속성을 사용해 색상 그리드 박스 만들기";
            SizeToContent = SizeToContent.WidthAndHeight;
            FontFamily    = new FontFamily("나눔고딕코딩");
            FontSize      = 16;

            StackPanel stackPanel = new StackPanel();

            stackPanel.Orientation = Orientation.Horizontal;

            Content = stackPanel;

            Button leftButton = new Button();

            leftButton.Content             = "Do-nothing button\nto test tabbing";
            leftButton.Margin              = new Thickness(24);
            leftButton.HorizontalAlignment = HorizontalAlignment.Center;
            leftButton.VerticalAlignment   = VerticalAlignment.Center;

            stackPanel.Children.Add(leftButton);

            ColorGridBox colorGridBox = new ColorGridBox();

            colorGridBox.Margin              = new Thickness(24);
            colorGridBox.HorizontalAlignment = HorizontalAlignment.Center;
            colorGridBox.VerticalAlignment   = VerticalAlignment.Center;

            stackPanel.Children.Add(colorGridBox);

            colorGridBox.SetBinding(ColorGridBox.SelectedValueProperty, "Background");

            colorGridBox.DataContext = this;

            Button rightButton = new Button();

            rightButton.Content             = "Do-nothing button\nto test tabbing";
            rightButton.Margin              = new Thickness(24);
            rightButton.HorizontalAlignment = HorizontalAlignment.Center;
            rightButton.VerticalAlignment   = VerticalAlignment.Center;

            stackPanel.Children.Add(rightButton);
        }

        #endregion

        //////////////////////////////////////////////////////////////////////////////////////////////////// Method
        ////////////////////////////////////////////////////////////////////////////////////////// Static
        //////////////////////////////////////////////////////////////////////////////// Public

        #region 프로그램 시작하기 - Main()

        /// <summary>
        /// 프로그램 시작하기
        /// </summary>
        [STAThread]
        public static void Main()
        {
            Application application = new Application();

            application.Run(new MainWindow());
        }

        #endregion
    }
}
728x90
반응형
그리드형(광고전용)
Posted by icodebroker

댓글을 달아 주세요