2016-11-28 157 views
2

我想添加一个新的自定义颜色到WPF颜色选择器的可用颜色。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

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

+2

根据[源代码](http://wpftoolkit.codeplex.com/SourceControl/latest#Main/Source/ExtendedWPFToolkitSolution/Src/Xceed.Wpf.Toolkit/Core/Utilities/ColorUtilities.cs),名称不是由'AvailableColors'中的条目决定的,而是由扩展方法'ColorUtilities.GetColorName'决定的。如果您也将颜色添加到ColorUtilities.KnownColors中,它可能会起作用。 –

+0

@ManfredRadlwimmer你可以请加这个作为答案,我会接受相同的,这个作品!,下载源代码!干杯, – Sandepku

回答

1

正如我上面我的评论中提及:

按照Source Code名称不被条目AvailableColors确定,但扩展方法ColorUtilities.GetColorName。如果您也将颜色添加到ColorUtilities.KnownColors,也许它会起作用。

A(脏)的解决方法,直至开发商解决这一问题将是忽略ColorUtilities类是私有的:

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