1

When I use ContextMenu from Silverlight 4 Toolkit I want to get ContextMenu instance's Owner, but it's not available since in ContextMenu class Owner is an internal property. For example, I have a Rectangle put inside a Border. When I right-click a Rectangle (and a context menu appears) I need to change the Rectangle Border's thickness to indicate that the Rectangle is the current element of the parent Grid, for example. So I try to use ContextMenu.Loaded event where I get the ContextMenu (through the sender parameter), but I can't get the Owner of that ContextMenu (i.e. the Rectangle with its Border). And I need that Owner to make its Border thicker. Please help me find the way to do that.

BillVo
  • 565
  • 1
  • 5
  • 20
Dmitriyz
  • 148
  • 1
  • 11
  • Maybe you mean the PlacementTarget property. – vortexwolf Mar 19 '11 at 10:36
  • to vorrtex: I can't find the PlacementTarget property among ContextMenu's members in Silverlight. Is it supposed to be there in Silverlight or is it just there in WPF? – Dmitriyz Mar 19 '11 at 14:07
  • @Dmitriyz I've confused Silverlight with WPF. But I know what to do: use reflection and read the private field _owner. – vortexwolf Mar 19 '11 at 14:49
  • Reflection doesn't work. It seems that it is impossible to get owner. – vortexwolf Mar 19 '11 at 15:25
  • I think also ContextMenu element handles MouseRightButtonDown event of the owner, so I can't even set needed border thickness in that event handler. If I define an event handler for the border it just never goes there. Are there any other ways to keep ContextMenu and still do what I need? – Dmitriyz Mar 19 '11 at 15:47
  • @Dmitryz You can add the x:Name attribute to the border and change the thickness of the border from an event handler of MenuItem. – vortexwolf Mar 19 '11 at 18:49
  • Thank you. I wish there were other ways to do that. – Dmitriyz Mar 20 '11 at 04:23
  • I found it! As I create my rectangles and their context menus dynamically I can set each rectangle's border's DataContext property explicitly like this: myContextMenu.Context = myBorder; myContextMenu.Loaded += new RoutedEventHandler(myContextMenu_Loaded); myBorder.SetValue(ContextMenuService.ContextMenuProperty, myContextMenu); ... Then in myContextMenu_Loaded function I can see my rectangle's border like this: Border neededBorder = (sender as ContextMenu).DataContext as Border; ... Thank you, vorrtex, for participating in my search :) – Dmitriyz Mar 20 '11 at 07:33

1 Answers1

1

The only soultion I found was to set ContextMenu's DataContext to the needed element from code behind. And then to address the element on which the context menu was opened throught that DataContext property.

Dmitriyz
  • 148
  • 1
  • 11