<Button Name="btnFoo" Content="Foo" >
<Button.ContextMenu Placement="Bottom" PlacementTarget="btnFoo">
<MenuItem Header="Bar" />
</Button.ContextMenu>
</Button>
gives me a runtime error 'UIElement' type does not have a public TypeConverter class
I also tried
<Button Name="btnFoo" Content="Foo" >
<Button.ContextMenu Placement="Bottom" PlacementTarget="{Binding ElementName=btnFoo}">
<MenuItem Header="Bar" />
</Button.ContextMenu>
</Button>
and that put the ContextMenu in the top left corner of my screen, rather than at the Button
You should be setting the ContextMenuService.Placement attached property on the button, as stated in the remarks in the documentation for ContextMenu.Placement.
You could use a
<Menu />
, styled as aButton
and avoid the hassle with theContextMenuService
.Have you tried this:
This will make the ContextMenu open where you right clicked your mouse (on the button). Which I think might be your desired location right?
--- EDIT --- In that case use this:
And in code behind:
You can reuse the method for multiple buttons and ContextMenu's..