0
votes

I am struggling to get this binding working on my ContextMenu.

I am using Items Source so each item is of the type frm ItemsSource, so I need to search up the tree to get the correct binding context.

The command I am trying to execute exists within the DataContext of the ContextMenu as you can see it bound here:

DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}"

So I attempt to bind to that data context using RelativeSource but no matter what I have tried I always get a binding error:

System.Windows.Data Error: 40 : BindingExpression path error: 'CreateNodeCommand' property not found on 'object' ''NodeGraphView' (Name='networkControl')'. BindingExpression:Path=DataContext.CreateNodeCommand; DataItem='ContextMenu' (Name='NodeGraphRightClickMenu'); target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')

Here is the code:

  <ContextMenu x:Name="NodeGraphRightClickMenu" DataContext="{Binding PlacementTarget, RelativeSource={RelativeSource Self}}" ItemsSource="{Binding DataContext.PolymorphicTypes}">
<ContextMenu.ItemContainerStyle>
    <Style TargetType="MenuItem">
        <Setter Property="Command" Value="{Binding DataContext.CreateNodeCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
        <Setter Property="CommandParameter">
            <Setter.Value>
                <MultiBinding Converter="{convertersDF:Converter_MultiBindingToArray}">
                    <Binding Path="."></Binding>
                    <Binding Path="Name"></Binding>
                </MultiBinding>
            </Setter.Value>
        </Setter>
        <Setter Property="Header">
            <Setter.Value>
                <Binding StringFormat="Create {0}" Path="Name"></Binding>
            </Setter.Value>
        </Setter>
    </Style>
</ContextMenu.ItemContainerStyle>

Any help much appreciated.

1
did u check the property name ? check any spell mistake is not thereDhru 'soni

1 Answers

0
votes

The answer was so obvious. I needed to bind to the DataContext of the DataContext. This was confusing so I have re-written it:

                                <ContextMenu x:Name="NodeGraphRightClickMenu" DataContext="{Binding PlacementTarget.DataContext, RelativeSource={RelativeSource Self}}" ItemsSource="{Binding PolymorphicTypes}">
                                    <ContextMenu.ItemContainerStyle>
                                        <Style TargetType="MenuItem">
                                            <Setter Property="Command" Value="{Binding DataContext.CreateNodeCommand, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"/>
                                            <Setter Property="CommandParameter">
                                                <Setter.Value>
                                                    <MultiBinding Converter="{convertersDF:Converter_MultiBindingToArray}">
                                                        <Binding Path="."></Binding>
                                                        <Binding Path="Name"></Binding>
                                                    </MultiBinding>
                                                </Setter.Value>
                                            </Setter>
                                            <Setter Property="Header">
                                                <Setter.Value>
                                                    <Binding StringFormat="Create {0}" Path="Name"></Binding>
                                                </Setter.Value>
                                            </Setter>
                                        </Style>
                                    </ContextMenu.ItemContainerStyle>
                                </ContextMenu>