开发者

Binding a context menu item with a dependency property

开发者 https://www.devze.com 2023-01-07 22:49 出处:网络
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 c

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 开发者_StackOverflow社区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>


just a quick guess:

have you tried setting your Contextmenu on DesignerItem the same Way as for the Canvas? like so:

<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=.}">

          <!-- other template parts -->

        </Grid>
      </ControlTemplate>
    </Setter.Value>
  </Setter>
  <Setter
    Property="ContextMenu"
    Value="{StaticResource DesignerItemContextMenu}" />
</Style>

other than that, I really recommend you fire up Snoop on your application and look for binding errors in your context menu

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号