向菜单项添加一个彩色的椭圆
问题描述:
我想为我的ContextMenu
的某些MenuItems
添加Ellipse
。向菜单项添加一个彩色的椭圆
可悲的是我无法得到这个工作[什么都没有显示]。
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);
我在想什么或错在哪里?
预期的结果是......。像这样:
答
不需要图像:Icon
是object
。它可以是任何内容:任何视觉元素,任何值,任何类的任何实例。如果它是一个视图模型,它将需要一个隐式的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
};
如果你想更复杂的东西,你可以给它Canvas
,随之Ellipse
和其他子元素。
+1
哦哇。这很简单:D - 非常感谢 –
*“无法让这个工作”*看起来像什么?不过你缺少[编码器](https://wpftutorial.net/BitmapFromVisual.html)。 – Sinatr