1

Setup

I have a series of drag and drop elements (WPF Controls) on a template. When those template elements are stacked I offer the user the ability to unstack them through the context menu. The context menu items are generated in the behind code in an extension class I created.

Problem

I would like to offer an image of the element as the icon in the context menu, next to the corresponding menu item header. Currently I have tried a series of solutions, none of which work.

Atempted solutions

How can I convert a WPF control into an image?

Get a bitmap image from a Control view

The MenuItem.Icon takes a visual object. Unfortunately when I just hand it my element, which is a visual, it bombs because the element is part of another visual tree.

So I tried cloaning it and then providing it the same DataContext. Which worked, kinda. Without going into to much depth, the size of the element is bound to the datacontext so it's VERY difficult to adjust the size from the behind code without it looking like a giant hack.

Desired outcome

I want to render an image of my WPF control and assign that to the MenuItem.Icon property. Also I need to be able to do this in the behind code.

Community
  • 1
  • 1
DotNetRussell
  • 9,716
  • 10
  • 56
  • 111
  • Is the problem that you can't change the visual size (width and height) of the image being used as the binding source? You've mentioned several goals, but the problem isn't very clear to me. – Aaron Thomas Jun 09 '15 at 14:40
  • No, I was just trying to explain why setting the visual object itself to the MenuItem.Icon wasn't an option. The problem is that I want to turn my WPF Control into an Image, then scale that image appropriately and finally set that to the MenItem.Icon – DotNetRussell Jun 09 '15 at 14:55

1 Answers1

0

To get an image of a control, you can do using a VisualBrush, with Visual bound to the control. An example of this with transforms (stretching, etc) can be found at MSDN here.

Aaron Thomas
  • 5,054
  • 8
  • 43
  • 89
  • After inspecting the links you've provided above, I want to point out my answer involves making a visual of the control that you can use in-code, bypassing the need to create an image of the control, to save it, and then to load that image for use. – Aaron Thomas Jun 09 '15 at 15:19