私はコンテキストメニューの使い方を学んでいます。私はメニューを取得し、それは良いようですが、コマンドはコンテキストメニューにバインドしません。コンテキストメニューの下の目に見えるボタンスタックパネルで機能します。私は、出力でこれを取得する:コンテキストメニューのコマンドのバインド
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>
Thakeこれを見ます。http:// stackoverflowの。 com/questions/3878620/binding-context-menu-item-to-parent-control – CiccioRocca