Im trying to learn how to use a contextmenu. I get the menu and it looks good but the commands do not bind in the contextmenu. They do work in the visible button stack panel below the contextmenu. I get this in the output :
System.Windows.Data Error: 4 : Cannot find source for binding with reference 'ElementName=MainGrid'. BindingExpression:Path=DataContext.StartClientCommand; DataItem=null; target element is 'MenuItem' (Name=''); target property is 'Command' (type 'ICommand')
<ListBox x:Name="lbSlaves" Grid.Row="1"
ItemsSource="{Binding Slaves}" ScrollViewer.HorizontalScrollBarVisibility="Disabled" >
<ListBox.ItemsPanel>
<ItemsPanelTemplate>
<WrapPanel />
</ItemsPanelTemplate>
</ListBox.ItemsPanel>
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel Orientation="Vertical" Width="150" >
<StackPanel Orientation="Horizontal">
<CheckBox IsChecked="{Binding IsSelected ,Mode=TwoWay}"/>
<TextBlock Text="{Binding FriendlyName, Mode=OneWay}" >
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Start"
Command="{Binding ElementName=MainGrid, Path=DataContext.StartClientCommand}"
CommandParameter="{Binding}" />
<MenuItem Header="Stop"
Command="{Binding ElementName=MainGrid, Path=DataContext.StopClientCommand}"
CommandParameter="{Binding}" />
<MenuItem Header="Calibrate"
Command="{Binding ElementName=MainGrid, Path=DataContext.CalibrateClientCommand}"
CommandParameter="{Binding}" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</StackPanel>
<Button
Content="Start"
Command="{Binding ElementName=MainGrid, Path=DataContext.StartClientCommand}"
CommandParameter="{Binding}" />
<Button
Content="Stop"
Command="{Binding ElementName=MainGrid, Path=DataContext.StopClientCommand}"
CommandParameter="{Binding}" />
<Button
Content="Calibrate"
Command="{Binding ElementName=MainGrid, Path=DataContext.CalibrateClientCommand}"
CommandParameter="{Binding}" />
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>