728x90
반응형

 

1.

 

using System.Reflection;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Media;

 

namespace WpfApp

{

    /// <summary>

    /// MainWindow.xaml에 대한 상호 작용 논리

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

 

            Title = "List Color Names";

 

            // 윈도우의 목록처럼 ListBox를 생성

            ListBox listbox = new ListBox();

            listbox.Width = 150;

            listbox.Height = 150;

 

            Content = listbox;

            // 색상명으로 ListBox를 채움

            PropertyInfo[] props = typeof(Colors).GetProperties();

            foreach (PropertyInfo prop in props)

            {

                listbox.Items.Add(new { NAME = prop.Name, BRUSH = new SolidColorBrush((Color)prop.GetValue(null, null)) });

            }

 

            listbox.DisplayMemberPath = "NAME";

            listbox.SelectedValuePath = "BRUSH";

 

            // SelectedValue와 윈도우 배경색을 바인딩

            listbox.SetBinding(ListBox.SelectedValueProperty, nameof(List.Background));

            listbox.DataContext = this;

        }

    }

}

 

 

 

 

2.

 

using System.Reflection;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Documents;

using System.Windows.Media;

 

namespace WpfApp2

{

    /// <summary>

    /// MainWindow.xaml에 대한 상호 작용 논리

    /// </summary>

    public partial class MainWindow : Window

    {

        public MainWindow()

        {

            InitializeComponent();

 

            Title = "List Color";

 

            // 윈도우 Content를 위한 리스트 박스 생성

            ListBox listbox = new ListBox();

            listbox.Width = 150;

            listbox.Height = 150;

 

            Content = listbox;

 

            // label 컨트롤로 리스트 박스를 채움

            PropertyInfo[] props = typeof(Colors).GetProperties();

 

            foreach (PropertyInfo prop in props)

            {

                Color color = (Color)prop.GetValue(null, null);

 

                bool isBlack = .222 * color.R + .707 * color.G + .071 * color.B > 128;

 

                Label label = new Label();

                label.Content = prop.Name;

                label.Background = new SolidColorBrush(color);

                label.Foreground = isBlack ? Brushes.Black : Brushes.White;

                label.Width = 100;

                label.Margin = new Thickness(15, 0, 0, 0);

                label.Tag = new SolidColorBrush(color);

 

                listbox.Items.Add(label);

            }

 

            listbox.SelectedValuePath = nameof(Label.Tag);

            listbox.SetBinding(ListBox.SelectedValueProperty, nameof(List.Background));

            listbox.DataContext = this;

        }

    }

}

 

 

728x90
반응형

+ Recent posts