■ ListBox 클래스 : ItemTemplate 속성을 사용해 색상 리스트 박스 만들기

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

using System;

using System.Windows;

using System.Windows.Controls;

using System.Windows.Data;

using System.Windows.Shapes;

 

DataTemplate dataTemplate = new DataTemplate(typeof(BrushName));

 

FrameworkElementFactory stackPanelFrameworkElementFactory = new FrameworkElementFactory(typeof(StackPanel));

 

stackPanelFrameworkElementFactory.SetValue(StackPanel.OrientationProperty, Orientation.Horizontal);

 

dataTemplate.VisualTree = stackPanelFrameworkElementFactory;

 

FrameworkElementFactory rectangleFrameworkElementFactory = new FrameworkElementFactory(typeof(Rectangle));

 

rectangleFrameworkElementFactory.SetValue(Rectangle.WidthProperty , 16.0);

rectangleFrameworkElementFactory.SetValue(Rectangle.HeightProperty, 16.0);

rectangleFrameworkElementFactory.SetValue(Rectangle.MarginProperty, new Thickness(2));

rectangleFrameworkElementFactory.SetValue(Rectangle.StrokeProperty, SystemColors.WindowTextBrush);

 

rectangleFrameworkElementFactory.SetBinding(Rectangle.FillProperty, new Binding("Brush"));

 

stackPanelFrameworkElementFactory.AppendChild(rectangleFrameworkElementFactory);

 

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

 

textBlockFrameworkElementFactory.SetValue(TextBlock.VerticalAlignmentProperty, VerticalAlignment.Center);

textBlockFrameworkElementFactory.SetValue(TextBlock.TextProperty             , new Binding("Name"));

textBlockFrameworkElementFactory.SetValue(TextBlock.MarginProperty           , new Thickness(2, 0, 0, 0));

 

stackPanelFrameworkElementFactory.AppendChild(textBlockFrameworkElementFactory);

 

ListBox listBox = new ListBox();

 

listBox.Width  = 150;

listBox.Height = 150;

 

Content = listBox;

 

listBox.ItemTemplate = dataTemplate;

 

listBox.ItemsSource = BrushName.All;

 

listBox.SelectedValuePath = "Brush";

 

listBox.SetBinding(ListBox.SelectedValueProperty, "Background");

 

listBox.DataContext = this;

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

BrushName : "BrushName 클래스 만들기" 참조

Posted by 사용자 icodebroker