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

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

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


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

    }

}

 

 

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

    }

}

 

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

Posted by 사용자 icodebroker
TAG

댓글을 달아 주세요