由组合框组成的非常简单的颜色选择器
Very simple color picker made of combobox
这是我的 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>
这是 c# 中的一组组合框项目:
comboColors.ItemsSource = typeof(Colors).GetProperties();
现在的问题是 - 如何让这个组合在 drop-down 列表 4-5-6 或更多列中显示它的项目?
另一个问题 - 我如何为这个下拉列表插入标题?
说 "palett colors are:"
这是一个文本字段 - 不是一对颜色名称
也许我可以添加透明颜色+标题作为第一个元素
但如何让它成为第一排?
可能是一个数据网格,因为下拉菜单是个不错的主意?我现在就试试)
在你的.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>
在你的.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);
}
}
}
这是我的 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>
这是 c# 中的一组组合框项目:
comboColors.ItemsSource = typeof(Colors).GetProperties();
现在的问题是 - 如何让这个组合在 drop-down 列表 4-5-6 或更多列中显示它的项目?
另一个问题 - 我如何为这个下拉列表插入标题? 说 "palett colors are:" 这是一个文本字段 - 不是一对颜色名称 也许我可以添加透明颜色+标题作为第一个元素 但如何让它成为第一排?
可能是一个数据网格,因为下拉菜单是个不错的主意?我现在就试试)
在你的.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>
在你的.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);
}
}
}