Ok, I have figured out a way to do this using a Converter. Firstly, the XAML code to specify the Color Selector inside a DropDown
Secondly, the Converter:
public class selectedColorConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
int Width = 32;
int Height = 32;
Color newcolor = (Color)value;
if (parameter != null && System.Convert.ToInt32(parameter.ToString()) == 16)
{
Width = 16;
Height = 16;
}
DrawingVisual dv = new DrawingVisual();
using (DrawingContext ctx = dv.RenderOpen())
{
SolidColorBrush vb = new SolidColorBrush(newcolor);
Pen pen = new Pen(new SolidColorBrush(Colors.Black), 1.0f);
//Add a margin for 32 x 32 so it looks less imposing.
Rect rect = new Rect(new Point(4, 4), new Size(24, 24));
if (parameter != null && System.Convert.ToInt32(parameter.ToString()) == 16)
{
//For some reason 16 x 16 was chopping off the edge, probably to do with rounding.
rect = new Rect(new Point(1, 1), new Size(14, 14));
}
//Prevent Blurred border
double halfPenWidth = pen.Thickness / 2;
GuidelineSet guidelines = new GuidelineSet();
guidelines.GuidelinesX.Add(rect.Left + halfPenWidth);
guidelines.GuidelinesX.Add(rect.Right + halfPenWidth);
guidelines.GuidelinesY.Add(rect.Top + halfPenWidth);
guidelines.GuidelinesY.Add(rect.Bottom + halfPenWidth);
ctx.PushGuidelineSet(guidelines);
ctx.DrawRectangle(vb, pen, rect);
ctx.Pop();
}
RenderTargetBitmap rtb = new RenderTargetBitmap(Width, Height, 96, 96, PixelFormats.Pbgra32);
rtb.Render(dv);
return rtb;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
throw new System.NotImplementedException();
}
}