イメージはこんな感じ。
全部 C# で書いているが、ItemTemplate を xaml で書けば、Siliverlight でも使える。ただし、Siliverlight に Brushes が無いので Colors を使うことになると思うが。
/// <summary>
/// カラーピッカー
/// </summary>
public class ColorPicker : ComboBox
{
/// <summary>
/// コンストラクタ
/// </summary>
public ColorPicker()
{
// テンプレート設定
this.ItemTemplate = getItemTemplate();
this.ItemsPanel = getItemsPanelTemplate();
// Brushes からカラーのリストを生成
List<Brush> list = (from brush in typeof(Brushes).GetProperties() select (Brush)brush.GetValue(null, null)).ToList();
list.Sort(delegate(Brush source, Brush target) { return source.ToString().CompareTo(target.ToString()); });
this.ItemsSource = list;
this.Loaded += new RoutedEventHandler(ColorPicker_Loaded);
}
/// <summary>
/// ロード完了イベント
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void ColorPicker_Loaded(object sender, RoutedEventArgs e)
{
this.SelectedItem = Brushes.Black;
}
/// <summary>
/// ItemTemplate取得
/// </summary>
/// <returns></returns>
private DataTemplate getItemTemplate()
{
DataTemplate dataTemplate = new DataTemplate();
FrameworkElementFactory element = new FrameworkElementFactory(typeof(Border));
element.SetValue(Border.BorderBrushProperty, Brushes.Black);
element.SetValue(Border.BorderThicknessProperty, new Thickness(1.0));
element.SetValue(Border.VerticalAlignmentProperty, VerticalAlignment.Center);
element.SetValue(Border.WidthProperty, 18.0);
element.SetValue(Border.HeightProperty, 18.0);
element.SetValue(Border.MarginProperty, new Thickness(0, 2, 0, 2));
element.SetBinding(Border.BackgroundProperty, new Binding());
dataTemplate.VisualTree = element;
return dataTemplate;
}
/// <summary>
/// ItemsPanelTemplate取得
/// </summary>
/// <returns></returns>
private ItemsPanelTemplate getItemsPanelTemplate()
{
ItemsPanelTemplate panel = new ItemsPanelTemplate();
FrameworkElementFactory element = new FrameworkElementFactory(typeof(WrapPanel));
element.SetValue(WrapPanel.WidthProperty, 150.0);
element.SetValue(WrapPanel.OrientationProperty, Orientation.Horizontal);
panel.VisualTree = element;
return panel;
}
}
0 件のコメント:
コメントを投稿