我有一個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