Directly on the code, the first is a ContextMenu template:
<ContextMenux:key= "Menu"borderthickness= "0.3"FontSize= "+"DataContext="{Binding placementtarget.tag, Relativesource={relativesource self}}"> <MenuItemHeader= "Copy"Command="{Binding source={x:static Vm:viewmodellocator.instance},path=mainwindowvm.copyimagecommand}"CommandParameter="{Binding placementtarget.datacontext, Relativesource={relativesource Ancestortype=contextmenu}}"Commandtarget="{Binding placementtarget, Relativesource={relativesource self}}" /> <MenuItemHeader= "Save As ..."Command="{Binding source={x:static Vm:viewmodellocator.instance},path=mainwindowvm.saveimagecommand}"CommandParameter="{Binding placementtarget.datacontext, Relativesource={relativesource Ancestortype=contextmenu}}"Commandtarget="{Binding placementtarget, Relativesource={relativesource self}}" /> </ContextMenu>
Then there is the content of DataTemplate:
<DataTemplatex:key= "Customerimagedatatemplate"> <ButtonCommand="{Binding Datacontext.showimagecommand, Relativesource={relativesource FindAncestor, Ancestortype=window}}"Cursor= "Hand"CommandParameter="{Binding path=datacontext, Relativesource={relativesource mode=findancestor, ancestorlevel=1,ancestortype= StackPanel}}"Padding= "3"> <button.template> <ControlTemplateTargetType= "button"> <Grid> <BorderName= "Imgborder"BorderBrush="{StaticResource Listborder}"Cornerradius= "3"borderthickness= "0"Background="{dynamicresource {x:static Systemcolors.windowbrushkey}}"Margin= "0"> </Border> <Imagex:name= "Imagemsg"Source="{Binding Textcontent}"Stretch= "Uniform"Width= "+"Height= "a"ContextMenu="{StaticResource Menu}"Tag="{Binding}"> <Image.opacitymask> <VisualBrushVisual="{Binding elementname=imgborder, mode=onetime}" /> </Image.opacitymask> </Image> </Grid> </ControlTemplate> </button.template> </Button> </DataTemplate>
The button above also shows how to set the corners of the picture to rounded corners.
[WPF] Resolves a problem with ContextMenu binding commandparameter in a template