2017-02-10 157 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%的功能,我希望它能帮到你 –