views:

523

answers:

2

Hello,

I've been beating my head against a WPF Popup issue, and am hoping someone can help me:) The outline goes like this:

Our app stores a list of UIObjects - these represent elements that are displayed in a viewport 3d (via a member UIRenderObject, which is just a flat, square mesh with a bitmap on it) and in a treeview (via a member TreeViewItem).

In either case, when the object detects a right click, it tries to open a popup. The code for that is about as straight forward as you can get:

ContextMenu contextMenu = new ContextMenu();

//add content

contextMenu.IsOpen = true;

When I click in the viewport, everything works as expected - the popup opens, and stays open until it loses focus or a selection is made. In the tree-view, however, the popup loses focus and closes immediately.

I've tried explicitly setting focus, explicitly setting the PlacementTarget, marking the mouse event as handled before and after the call to open the popup, flat-out marking the popup as "StaysOpen" and a dozen other things, and still it closes immediately.

Any help will be much appreciated. Thanks in advance!

A: 

That is the default behavior of ContextMenu. Alternatively you can use Popup Class instead of ContextMenu. Then you can extend it as your requirement.

viky
Really? That seems very odd to me. It'd be one thing if it was consistent. But at the moment, it works as expected in one case, and closes immediately when opened through the same method from a slightly different source.I'll see what I can do with the Popup class, though. Thanks!
b34r
A: 

Sigh, as it turns out I was over-complicating the matter. Rather than managing the context menu myself, simply assigning it to TreeViewItem's own ContextMenu and letting the system deal with it, was the way to go. That'll learn me. Thanks for everyone's input :)

b34r