Hi,
I have a Custom Canvas(DesignerCanvas), user can add some controls to this canvas. Before adding any control canvas first wraps it insdie a custom control(Designer Item) and then add that control to canvas.
This is done as in this codeproject article -
WPF Diagram Designer - http://www.codeproject.com/KB/WPF/WPFDiagramDesigner_Part4.aspx
I have added context menus for both DesignerCanvas and DesignerItem having a menu item named 'Lock'. I have also added a property in canvas named "IsLocked" to lock the canvas.
Now, in the context menu I want to show a check mark if canvas is locked, to do this I have bound the IsLocked property to 'IsChecked' property of MenuItem. Problem is that it works for the DesignerCanvas cantext menu but not for DesignerItems context menu.
DesignerCanvas ContextMenu code, this works fine -
<!-- Context menu for DesignerCanvas -->
<ContextMenu x:Key="DesignerCanvasContextMenu">
<!-- Other menu items -->
<MenuItem IsCheckable="True" Header="Lock"
IsChecked="{Binding Path=IsLocked, Mode=TwoWay,
RelativeSource={RelativeSource AncestorType={x:Type locl:DesignerCanvas}}}"
Command="{x:Static local:DesignerCanvas.LockUnLock}"
CommandParameter="{Binding RelativeSource={RelativeSource Self},
Path=IsChecked}"
CommandTarget="{Binding Path=PlacementTarget,
RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}">
</MenuItem>
</ContextMenu>
Need to do achive same behavior for DesignerItem menu too. I tried doing this but it doesn't work -
<!-- Context menu for DesignerItem -->
<ContextMenu x:Key="DesignerItemContextMenu">
<!-- Other menu items -->
<MenuItem IsCheckable="True" Header="Lock"
IsChecked="{Binding Path=Parent.IsLocked, Mode=TwoWay,
RelativeSource={RelativeSource AncestorType={x:Type locl:DesignerItem}}}"
Command="{x:Static local:DesignerCanvas.LockUnLock}"
CommandParameter="{Binding RelativeSource={RelativeSource Self},
Path=IsChecked}"
CommandTarget="{Binding Path=PlacementTarget,
RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}}}">
</MenuItem>
</ContextMenu>
I tried to do this using PlacementTarget too but was not successful. As DesignerItem is DesignerCanvas's child it should not be impossible to get hold of the DesignerCanvas and in turn its property. What am I doing wrong, any idea?
Update:
I am attaching the context menu with DesignerCanvas and Designer Item like this -
<!-- DesignerItem || Style -->
<Style TargetType="{x:Type locl:DesignerItem}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type local:DesignerItem}">
<Grid x:Name="PART_Grid" Cursor="SizeAll"
DataContext="{Binding RelativeSource={RelativeSource TemplatedParent}
,Path=.}"
ContextMenu="{StaticResource DesignerItemContextMenu}">
<!-- other template parts -->
</Grid>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
<!-- DesignerCanvas || Style -->
<Style
TargetType="{x:Type locl:DesignerCanvas}">
<Setter
Property="ContextMenu"
Value="{StaticResource DesignerCanvasContextMenu}" />
</Style>