1
votes

This is the style for my ContextMenu:

<Style x:Key="DIOStyle" TargetType="ContentControl">
        <Setter Property="Tag" Value="{Binding Content,RelativeSource={RelativeSource Mode=Self}}"/>
        <Setter Property="ContextMenu">
            <Setter.Value>
                <ContextMenu>
                    <MenuItem Style="{StaticResource DeleteMenuItemStyle}"/>
                    <!--<MenuItem Header="Normality">
                        <MenuItem Style="{StaticResource NcMenuItemStyle}"/>
                        <MenuItem Style="{StaticResource NaMenuItemStyle}"/>
                    </MenuItem>-->
                    <MenuItem Style="{StaticResource BothContactsMenuItemStyle}"/>
                </ContextMenu>
            </Setter.Value>
        </Setter>
...

and this is the style for my MenuItem:

<Style x:Key="BothContactsMenuItemStyle" TargetType="MenuItem">
        <Setter Property="Header" Value="Both Contacts"/>
        <Setter Property="Command" Value="{Binding PlacementTarget.Tag.BothNaNcChangeCommand,RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}"/>
        <Setter Property="Icon">
            <Setter.Value>
                <Image Style="{StaticResource Tick16Style}" Visibility="{Binding PlacementTarget.Tag.BothNaNc, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}, Converter={StaticResource BoolToVis}}"/>
            </Setter.Value>
        </Setter>
    </Style>

Command binding is working, instead icon visibility binding give me this error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.ContextMenu', AncestorLevel='1''. BindingExpression:Path=PlacementTarget.Tag.BothNaNc; DataItem=null; target element is 'Image' (Name=''); target property is 'Visibility' (type 'Visibility')

Why? Please note, this is working:

<MenuItem Header="TEST" Visibility="{Binding PlacementTarget.Tag.BothNaNc, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}, Converter={StaticResource BoolToVis}}"/>
1

1 Answers

1
votes

You could define the Image element as a resource. This should work:

<Image x:Key="img" x:Shared="False"
               Style="{StaticResource Tick16Style}"
               Visibility="{Binding PlacementTarget.Tag.BothNaNc, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu},
                            Converter={StaticResource BoolToVis}}"/>

<Style x:Key="BothContactsMenuItemStyle" TargetType="MenuItem">
    <Setter Property="Header" Value="Both Contacts"/>
    <Setter Property="Command" Value="{Binding PlacementTarget.Tag.BothNaNcChangeCommand, RelativeSource={RelativeSource Mode=FindAncestor,AncestorType=ContextMenu}}"/>
    <Setter Property="Icon" Value="{StaticResource img}" />
</Style>

If you set the Icon property to an Image element that you define inline in <Setter.Value> like you are currently doing, it will inherit the DataContext from the parent Window or UserControl or wherever the Style is defined.