C# 用combobox制作的非常简单的颜色选择器
这是我的xaml:C# 用combobox制作的非常简单的颜色选择器,c#,wpf,combobox,C#,Wpf,Combobox,这是我的xaml: <ComboBox Name="comboColors"> <ComboBox.ItemTemplate> <DataTemplate> <StackPanel Orientation="Horizontal"> <Rectangle Fill="{
<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>
现在的问题是-如何使此组合在下拉列表4-5-6或更多列中显示其项
还有一个问题-我如何为这个下拉列表插入标题?
说“palett颜色是:
这是一个文本字段-不是一对颜色-名称
也许我可以添加透明颜色+标题作为第一个元素
但是如何让它成为第一排呢
可能是一个数据网格,因为下拉是一个很酷的主意?我现在就试试看)在您的.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);
}
}
}
。。。
私有静态只读int列=5;
...
comboColors.ItemsSource=typeof(Colors).GetProperties();
...
已加载私有无效表(对象发送方,路由目标e){
网格网格=发送方作为网格;
如果(网格!=null){
if(grid.RowDefinitions.Count==0){
对于(int r=0;r
...
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);
}
}
}