我不明白为什么当我使用MVVM时无法检测到右键单击我的列表框。我使用了事件触发器,但有些事件不起作用。
<ListBox x:Name="PlaylistsList" ItemsSource="{Binding PlaylistsList}" HorizontalAlignment="Left">
<i:Interaction.Triggers>
<i:EventTrigger EventName="MouseRightButtonDown">
<i:InvokeCommandAction Command="{Binding NewPlaylistCommand}"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</ListBox>我的命令"NewPlaylistCommand“从不调用。你能帮我吗?谢谢。
编辑:我找到了问题的解决方案,我使用ContextMenu与我的ListBoxItem进行交互
<ListBox.ItemTemplate>
<DataTemplate>
<StackPanel x:Name="ListBox">
<TextBlock Text="{Binding Name}"/>
<StackPanel.ContextMenu>
<ContextMenu>
<MenuItem Header="Rename" Command="{Binding RenamePlaylistCommand}"/>
<MenuItem Header="Delete" Command="{Binding DeletePlaylistCommand}" CommandParameter="{Binding SelectedValue, ElementName=PlaylistsList}"/>
</ContextMenu>
</StackPanel.ContextMenu>
</StackPanel>
</DataTemplate>
</ListBox.ItemTemplate>现在,我可以右键单击我的项目来重命名或删除它。谢谢你的帮助。
发布于 2013-04-02 00:57:12
最有可能的是,里面的某个东西已经吞噬了你的右键单击事件,因此它没有冒泡出来(我怀疑这与上下文菜单行为有关)。您可以使用Preview- events,它以另一种方式冒泡。
您可以使用的另一个解决方案是将交互直接应用于列表框中的每个孩子(以及他们的孩子)。
然而,老实说,从UI的角度来看,我不希望右键单击播放列表会创建一个新的播放列表……根据我多年的windows使用经验,我希望弹出一个上下文菜单,可能会有一个“添加新的播放列表”菜单项。
https://stackoverflow.com/questions/15747624
复制相似问题