我有一个ListView里面的MenuItem。我想要什么:当我点击ListView项目时,一些命令触发。这里是我的代码:ListView项单击事件MVVM
<MenuItem Header="?">
<ListView ItemsSource="{Binding CommentTemplateList}" BorderThickness="0" SelectedItem="{Binding SelectedCommentTemplate, UpdateSourceTrigger=PropertyChanged}">
<i:Interaction.Triggers>
<i:EventTrigger EventName="SelectionChanged">
<i:InvokeCommandAction Command="{Binding PasteTemplate}"
CommandParameter="{Binding SelectedCommentTemplate}" />
</i:EventTrigger>
</i:Interaction.Triggers>
<ListView.View>
<GridView>
<GridViewColumn>
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Caption}" ToolTip="{Binding Description}" HorizontalAlignment="Center"></TextBlock>
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
</MenuItem>
Everithing是好的,但命令PasteTemplate火灾只有当选择改变,我需要它触发每次我点击项目。如果我将EventName更改为列表中的一个(https://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.selector.aspx),例如MouseDown,则命令根本不会触发。
我使用MVVM,所以我已将ListView SelectedIndex属性绑定到SelectedTemplateIndex变量。 我的代码:XAML: '的SelectedIndex = “{结合SelectedTemplateIndex}”' 视图模型: '私人无效OnPasteTemplateExecute(){如果(SelectedTemplateIndex ==''-1)回报; LeaveCommentBox = SelectedCommentTemplate.Description;''SelectedTemplateIndex = -1; }' 而且我看到相同的行为 - 命令只有在选择实际发生变化时才会触发。 –
'SelectedTemplateIndex = -1'应该重置您的选择。它选择-1st元素(在列表视图中,这意味着什么都不选择),并让你再次选择。 如果它仍然没有重置,您可以尝试重置您的SelectedItem或检查此[链接](http:// stackoverflow。com/a/18462019/7538242) – GBursali