2

I would like to add an Ellipse to some MenuItems of my ContextMenu.

Sadly I could not get this to work [Nothing is displayed].

Canvas canvas = new Canvas() { Height = 16, Width = 16 };
canvas.Children.Add(new System.Windows.Shapes.Ellipse()
{
    Height = 16,
    Width = 16,
    Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red)
});
System.Windows.Media.Imaging.RenderTargetBitmap bmp = new System.Windows.Media.Imaging.RenderTargetBitmap((int)canvas.Width, (int)canvas.Height, 96, 96, System.Windows.Media.PixelFormats.Default);

bmp.Render(canvas);
MenuItem tmp = new MenuItem();
tmp.Header = "Away";
tmp.Icon = new System.Windows.Controls.Image()
{
     Source = bmp
};

AddContextMenuEntry(tmp);

What am I missing or what is wrong here ?

Expected result would be sth. like this:

                                                             enter image description here

Felix D.
  • 4,811
  • 8
  • 38
  • 72
  • How does *"could not get this to work"* looks like? You are missing [encoder](https://wpftutorial.net/BitmapFromVisual.html) though. – Sinatr Oct 12 '17 at 15:05

1 Answers1

5

No image required: Icon is object. It can be any content: Any visual element, any value, any instance of any class. If it's a viewmodel it'll need an implicit DataTemplate. But a red circle is a snap.

MenuItem tmp = new MenuItem();
tmp.Header = "Away";

tmp.Icon = new System.Windows.Shapes.Ellipse()
{
    Height = 16,
    Width = 16,
    Fill = System.Windows.Media.Brushes.Red
};

If you want something more complicated, you could have given it the Canvas instead, with the Ellipse and other child elements.