comboBoxColor.ItemsSource = new[]
{
Color.FromRgb(255, 255, 255),
Color.FromRgb(0, 255, 255),
Color.FromRgb(255, 0, 255),
Color.FromRgb(255, 255, 0),
Color.FromRgb(0, 0, 255),
Color.FromRgb(255, 0, 0),
Color.FromRgb(0, 255, 0),
Color.FromRgb(122, 255, 255),
Color.FromRgb(255, 122, 255),
Color.FromRgb(255, 255, 122),
Color.FromRgb(122, 122, 255),
Color.FromRgb(255, 122, 122),
Color.FromRgb(122, 255, 122),
Color.FromRgb(122, 122, 122),
Color.FromRgb(0, 0, 0),
};
<Window x:Class="WpfApplication18.Window1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:local="clr-namespace:WpfApplication18"
Title="Window1" Height="300" Width="300">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<!-- 色選択コンボボックス -->
<ComboBox Name="comboBoxColor" Width="50">
<ComboBox.Resources>
<!-- ColorからSolidColorBrushへの変換 -->
<local:ColorToBrushConverter x:Key="c2b" />
</ComboBox.Resources>
<ComboBox.ItemContainerStyle>
<Style TargetType="{x:Type ComboBoxItem}">
<Setter Property="Margin" Value="0" />
<Setter Property="Padding" Value="0" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
</Style>
</ComboBox.ItemContainerStyle>
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<!-- ComboBoxItemをホストするパネルの設定 -->
<UniformGrid Columns="5" HorizontalAlignment="Left" />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
<DataTemplate>
<!-- 表示項目のテンプレート -->
<Border Name="border" Padding="2.5">
<Rectangle Stroke="Black" StrokeThickness="1" Fill="{Binding Converter={StaticResource c2b}}" Width="15" Height="15"/>
</Border>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</Grid>
</Window>
using System;
using System.Windows.Data;
using System.Windows.Media;
namespace WpfApplication18
{
public class ColorToBrushConverter : IValueConverter
{
#region IValueConverter メンバ
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return new SolidColorBrush((Color)value);
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
#endregion
}
}