我想在WPF中右键单击ListBoxItems时显示ContextMenu。我尝试了下面的代码,但是ContextMenu显示ListBoxItems不在那里。我想要的只是在只有ListBoxItems的右键单击时才显示ContextMenu。WPF中ListBox的上下文菜单项
<Grid>
<ListBox x:Name="listofConnectedItems" Grid.Column="0" Grid.Row="0" ItemsSource="{Binding MyItems}" MouseRightButtonDown="listofConnectedItems_MouseRightButtonDown" ContextMenuOpening="listofConnectedItems_ContextMenuOpening" >
<ListBox.ItemContainerStyle>
<Style TargetType="{x:Type ListBoxItem}">
<Setter Property="Padding" Value="10">
</Setter>
</Style>
</ListBox.ItemContainerStyle>
<ListBox.ContextMenu>
<ContextMenu>
<MenuItem Header="_Start" Click="MenuItemStart_Click" />
<MenuItem Header="Sto_p" Click="MenuItemStop_Click" />
<MenuItem Header="_Clear" Click="MenuItemClear_Click" />
</ContextMenu>
</ListBox.ContextMenu>
</ListBox>
</Grid>
你尝试将它设置在'ItemsContainerStyle'为'ListBoxItem'而不是整个'ListBox' – dkozl
您现在正在为整个LB设置CM。 – AnjumSKhan
在ListBoxItem中找不到@AnjumSKhan CM属性。这就是我问的,如何将CM只放置到ListBoxItems。 – Gopi