这里有一些代码会产生多种颜色。 (它将ComboBox
绑定到生成的颜色列表)。
以下代码不错。
xaml
<Window x:Class="WpfApplication1.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<TextBox x:Name="TextBox1" Width="300" Height="30" VerticalAlignment="Top" />
<ComboBox Margin="15" Height="30" Width="200" ItemsSource="{Binding ColorList}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" SelectionChanged="ComboBox_SelectionChanged">
<ComboBox.ItemTemplate>
<DataTemplate>
<Grid>
<Rectangle Height="55" Width="55" x:Name="Rectangle1">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Color}"/>
</Rectangle.Fill>
</Rectangle>
</Grid>
</DataTemplate>
</ComboBox.ItemTemplate>
<ComboBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel IsItemsHost="True" Orientation="Horizontal" Width="1000"/>
</ItemsPanelTemplate>
</ComboBox.ItemsPanel>
</ComboBox>
</Grid>
</Window>
C#
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.ComponentModel;
namespace WpfApplication1
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
public MainWindow()
{
this.InitializeComponent();
this.DataContext = this;
ColorList = ColorUtil.GenerateColorList(16);
}
private List<ColorWithInfo> colorList;
public List<ColorWithInfo> ColorList
{
get { return colorList; }
protected set
{
colorList = value;
RaisePropertyChanged("ColorList");
}
}
// INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged = delegate { };
private void RaisePropertyChanged(string propName)
{
PropertyChanged(this, new PropertyChangedEventArgs(propName));
}
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
TextBox1.Text = "Show Hex color here, for example #0000ff ";
}
}
public class ColorWithInfo : IComparable
{
public Color Color { get; set; }
public string Info
{
get { return string.Format("{0}/{1}/{2}", Color.R, Color.G, Color.B); }
}
public string HueSatBright
{
get { return string.Format("{0}/{1}/{2}", Hue, Saturation, Brightness); }
}
public float Hue
{
get { return System.Drawing.Color.FromArgb(Color.R, Color.G, Color.B).GetHue(); }
}
public float Saturation
{
get { return System.Drawing.Color.FromArgb(Color.R, Color.G, Color.B).GetSaturation(); }
}
public float Brightness
{
get { return System.Drawing.Color.FromArgb(Color.R, Color.G, Color.B).GetBrightness(); }
}
public int CompareTo(object obj)
{
ColorWithInfo cwiOther = obj as ColorWithInfo;
// Sort by Hue, and then Saturation, and then Brightness
if (this.Hue == cwiOther.Hue)
{
if (this.Saturation == cwiOther.Saturation)
return this.Brightness.CompareTo(cwiOther.Brightness);
else
return this.Saturation.CompareTo(cwiOther.Saturation);
}
else
return this.Hue.CompareTo(cwiOther.Hue);
}
}
public static class ColorUtil
{
public static List<ColorWithInfo> GenerateColorList(int numValsPerColor)
{
List<ColorWithInfo> colorList = new List<ColorWithInfo>();
// Create increment such that we start at 0, end at 255,
// and have a total of numValsPerColor within that range.
int delta = Convert.ToInt32(255.0 / ((double)numValsPerColor - 1.0));
for (int r = 0; r < numValsPerColor; r++)
for (int g = 0; g < numValsPerColor; g++)
for (int b = 0; b < numValsPerColor; b++)
{
ColorWithInfo cwi = new ColorWithInfo
{
Color = Color.FromRgb((byte)(r * delta), (byte)(g * delta), (byte)(b * delta))
};
colorList.Add(cwi);
}
colorList.Sort();
return colorList;
}
}
}
我的问题:
我想在TextBox1中看到选定的颜色十六进制字符串。
因此,可以说我单击了ComboBox
,然后在ComboBox
下拉菜单中单击了蓝色。如何在TextBox1
中看到#0000FF ?
答案 0 :(得分:0)
尝试一下:
private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (e.AddedItems.Count > 0)
{
var selectedItem = (ColorWithInfo)e.AddedItems[0];
TextBox1.Text = selectedItem.Color.ToString();
}
}
答案 1 :(得分:0)
要从评论中回答您的其他问题... 要将十六进制字符串添加到ComboBox中的每个项目,只需更改DataTemplate使其包含一个TextBlock并将其绑定到Color ...
<ComboBox.ItemTemplate>
<DataTemplate>
<StackPanel>
<Rectangle Height="55" Width="55" x:Name="Rectangle1">
<Rectangle.Fill>
<SolidColorBrush Color="{Binding Color}"/>
</Rectangle.Fill>
</Rectangle>
<TextBlock Text="{Binding Color}"/>
</StackPanel>
</DataTemplate>
</ComboBox.ItemTemplate>
注意:我也很简单地将网格更改为水平StackPanel,以使其快速适应。当然,您仍然可以使用Grid或至少使StackPanel看起来更好,但我会留给您。