2017-02-10 156 views
0

我有一個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,則命令根本不會觸發。

回答

-1

如果您想使用'SelectionChanged',您可以在代碼後重置選擇。只需在您的PasteTemplate上添加即可

if(((ListView)sender).SelectedIndex == -1)return; 
//your code 
((ListView)sender).SelectedIndex = -1; 

因此,在您的代碼之後,ListView沒有選定的元素。所以如果再次點擊它,選擇會再次改變,代碼會再次觸發。

注意:您也可以使用MouseDown,但它有點棘手。例如,如果用戶沒有單擊任何項​​目,而是在您的ListView內的其他地方(如this)單擊它,它會再次觸發當前選擇。

+0

我使用MVVM,所以我已將ListView SelectedIndex屬性綁定到SelectedTemplateIndex變量。 我的代碼:XAML: '的SelectedIndex = 「{結合SelectedTemplateIndex}」' 視圖模型: '私人無效OnPasteTemplateExecute(){如果(SelectedTemplateIndex ==''-1)回報; LeaveCommentBox = SelectedCommentTemplate.Description;''SelectedTemplateIndex = -1; }' 而且我看到相同的行爲 - 命令只有在選擇實際發生變化時纔會觸發。 –

+0

'SelectedTemplateIndex = -1'應該重置您的選擇。它選擇-1st元素(在列表視圖中,這意味着什麼都不選擇),並讓你再次選擇。 如果它仍然沒有重置,您可以嘗試重置您的SelectedItem或檢查此[鏈接](http:// stackoverflow。com/a/18462019/7538242) – GBursali

0

你可以處理ListViewItem預覽 MouseDown事件的建議在這裏:

WPF MVVM Light Multiple ListBoxItems bound to same object

<ListView ...> 
    <ListView.ItemContainerStyle> 
     <Style TargetType="ListViewItem"> 
      <EventSetter Event="PreviewMouseLeftButtonDown" Handler="OnMouseLeftButtonDown"/> 
     </Style> 
    </ListView.ItemContainerStyle> 
    .. 
</ListView> 

如果你不想從代碼 - 調用視圖模型的命令在你身後可以用相同的功能包裝相同的功能:https://www.codeproject.com/articles/28959/introduction-to-attached-behaviors-in-wpf

在上面的鏈接中有更多信息的例子。

0

要做到這一點,在尊重MVVM架構的同時,最好的方法是將具體行爲添加到xaml代碼中,如下所示;

<ListView x:Name="ListView" 
          ItemsSource="{x:Bind ViewModel.SampleItems, Mode=OneWay}" 
          SelectedItem="{x:Bind ViewModel.SelectedItem, Mode=OneWay}" 
          IsItemClickEnabled="True"> 
         <i:Interaction.Behaviors> 
          <ic:EventTriggerBehavior EventName="ItemClick"> 
           <ic:InvokeCommandAction Command="{x:Bind ViewModel.ItemClickCommand}" /> 
          </ic:EventTriggerBehavior> 
         </i:Interaction.Behaviors> 

        </ListView> 

而在你的視圖模型,宣告一個IComand財產後如下,

public ICommand ItemClickCommand 
    { 
     get 
     { 
      if (_itemClickCommand == null) 
      { 
       _itemClickCommand = new RelayCommand<ItemClickEventArgs>(OnItemClick); 
      } 

      return _itemClickCommand; 
     } 
    } 

定義,如果你是後面處理的代碼中的事件,如下的命令;

private void OnItemClick(ItemClickEventArgs args) 
{ListDataItem item = args?.ClickedItem as ListDataItem; //DO what ever you want with the Item you selected in the click} 

注:RelayCommand用於使用MVVMLight框架處理的命令。

+0

這對我來說是100%的功能,我希望它能幫到你 –