gpt4 book ai didi

c# - WPF 颜色选择器 - 添加新的自定义颜色

转载 作者:太空宇宙 更新时间:2023-11-03 12:36:19 28 4
gpt4 key购买 nike

我正在尝试将新的自定义颜色添加到 WPF 颜色选择器的可用颜色中。

代码隐藏

this.colorPicker1.AvailableColors.Add(new ColorItem(Color.FromArgb(255, 153, 153, 187), "Custom"));

XAML

<exceedToolkit:ColorPicker Name="colorPicker1" DisplayColorAndName="True"/>

enter image description here

问题是当我选择这个自定义颜色时,文本框显示十六进制值而不是颜色名称(“自定义”),我有办法解决这个问题吗?

最佳答案

正如我在上面的评论中提到的:

According to the Source Code the name isn't determined by the entries in AvailableColors but the extension method ColorUtilities.GetColorName. Maybe it will work if you add your color to ColorUtilities.KnownColors too.

在开发人员修复此问题之前的(肮脏的)解决方法是忽略 ColorUtilities 类是私有(private)的:

public static class ColorItemExtension
{
public static bool Register(this ColorItem colorItem)
{
if (colorItem.Color == null) return false;

Assembly assembly = typeof(ColorPicker).Assembly;
Type colorUtilityType = assembly.GetType("Xceed.Wpf.Toolkit.Core.Utilities.ColorUtilities");
if (colorUtilityType == null) return false;

FieldInfo fieldInfo = colorUtilityType.GetField("KnownColors");
if (fieldInfo == null) return false;

Dictionary<string, Color> knownColors = fieldInfo.GetValue(null) as Dictionary<string, Color>;
if (knownColors == null) return false;
if (knownColors.ContainsKey(colorItem.Name)) return false;

knownColors.Add(colorItem.Name, (Color) colorItem.Color);
return true;
}
}

可以这样使用:

var colorItem = new ColorItem(Color.FromRgb(1, 2, 3), "Almost black");
colorItem.Register();
colorPicker1.AvailableColors.Add(colorItem);

如果这对您很重要,您可能需要考虑将此问题提请开发人员注意 here

关于c# - WPF 颜色选择器 - 添加新的自定义颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40844496/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com