Reputation: 4741
This is my xaml:
<ComboBox Name="comboColors">
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Name}" Width="16" Height="16" Margin="0,2,5,2" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
And here is a population of combobox items in c#:
comboColors.ItemsSource = typeof(Colors).GetProperties();
Now the question is - how to make this combo show its items in drop-down list 4-5-6 or more columns?
And another question - how could I insert a title for this drop down? Say "palett colors are:" This is a text field - not a pair of color- name perhaps I could add transparent color + title as first element but how to make it be i a first row?
May be a datagrid as dropdown is a cool idea? I ll try it now)
Upvotes: 2
Views: 6672
Reputation: 962
In your .xaml
<ComboBox Name="comboColors">
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<Grid Loaded="table_Loaded" />
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Horizontal">
<Rectangle Fill="{Binding Name}" Width="16" Height="16" Margin="0 2 5 2" />
<TextBlock Text="{Binding Name}" />
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
And in your .xaml.cs
...
private static readonly int COLUMS = 5;
...
comboColors.ItemsSource = typeof(Colors).GetProperties();
...
private void table_Loaded(object sender, RoutedEventArgs e) {
Grid grid = sender as Grid;
if (grid != null) {
if (grid.RowDefinitions.Count == 0) {
for (int r = 0; r <= comboColors.Items.Count / COLUMS; r++) {
grid.RowDefinitions.Add(new RowDefinition());
}
}
if (grid.ColumnDefinitions.Count == 0) {
for (int c = 0; c < Math.Min(comboColors.Items.Count, COLUMS); c++) {
grid.ColumnDefinitions.Add(new ColumnDefinition());
}
}
for (int i = 0; i < grid.Children.Count; i++) {
Grid.SetColumn(grid.Children[i], i % COLUMS);
Grid.SetRow(grid.Children[i], i / COLUMS);
}
}
}
Upvotes: 7