向MenuItem添加彩色椭圆

Fel*_* D. 2 c# wpf menu menuitem bitmapimage

我想补充Ellipse一些MenuItems我的ContextMenu.

可悲的是,我无法让这个工作[ 没有显示 ].

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);
Run Code Online (Sandbox Code Playgroud)

我错过了什么或者这里有什么问题?

预期结果将是...... 像这样:

                                                             在此输入图像描述

15e*_*153 5

无需图像:Iconobject.它可以是任何内容:任何可视元素,任何值,任何类的任何实例.如果它是一个viewmodel,它将需要一个隐式的DataTemplate.但是一个红色的圆圈很容易.

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

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

如果你想要更复杂的东西,你可以Canvas用它Ellipse和其他子元素代替它.