1

I want to Bind the e.g. "Debug|Any CPU" MenuItem Command which has its own BuildConfiguration Context to the Project Context BuildProjectCommand. How to do it correctly?

I already tried to accomplish this with FindAncestor but it won't work. e.g. Command="{Binding Path=DataContext.BuildCommand, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type MenuItem}}}"

See picture for more understanding: enter image description here

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
killika
  • 72
  • 7
  • Binding popup menus can get tricky because of the way they are "disconnected" from the visual tree, they don't inherit the `DataContext` in the way you would expect. There are a few [tricks](https://stackoverflow.com/questions/662164/wpf-context-menu-doesnt-bind-to-right-databound-item) you can use though. – Bradley Uffner Sep 27 '17 at 17:11

1 Answers1

2

I've made a quick prototype and binding below should work as you expect:

<DataTemplate DataType="{x:Type commmon:BuildConfiguration}">
    <TextBlock x:Name="ConfigBlock">
        <Run Text="{Binding Name, Mode=OneWay}"/>
        <TextBlock.InputBindings>
            <MouseBinding MouseAction="LeftClick" 
                          Command="{Binding DataContext.BuildCommand, 
                          RelativeSource={RelativeSource AncestorLevel=2, 
                          AncestorType={x:Type MenuItem}}}">
            </MouseBinding>
        </TextBlock.InputBindings>
    </TextBlock>
</DataTemplate>
shadow32
  • 464
  • 3
  • 9