ListViewItem MouseDoubleClick MVVM方式

时间:2013-11-04 01:02:24

标签: c# .net wpf mvvm mvvm-light

我正在将我的项目转换为使用MVVM Light。

到目前为止,一切正常,直到我遇到将ListViewItem MouseDoubleClick绑定到命令的问题。

现在看起来像是:

<ListView x:Name="ItemsFromStash" Grid.Column="0" Grid.Row="1" 
      VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
      ItemsSource="{Binding DropBox.DroppedItems}" 
      ItemTemplate="{DynamicResource DropItemTemplate}"
      SelectedItem="{Binding DropBox.SelectedDropItem}">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <EventSetter Event="Control.MouseDoubleClick" 
                         Handler="EventSetter_OnHandler"/>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

我想让它看起来像那样:

<ListView x:Name="ItemsFromStash" Grid.Column="0" Grid.Row="1" 
          VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
          ItemsSource="{Binding DropBox.DroppedItems}" 
          ItemTemplate="{DynamicResource DropItemTemplate}"
          SelectedItem="{Binding DropBox.SelectedDropItem}">
    <ListView.ItemContainerStyle>
        <Style TargetType="{x:Type ListViewItem}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="Click">
                    <Custom:EventToCommand Command=
                        "{Binding DropBox.RenameItemCommand, Mode=OneWay}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Style>
    </ListView.ItemContainerStyle>
</ListView>

但它说:

  

属性'触发'不能附加到'Style'类型的元素

我尝试将命令移动到ListView.MouseDoubleClick,但有时候SelectedItem是null。

应该怎么做?

3 个答案:

答案 0 :(得分:1)

查看:

 <ListView x:Name="lw" ItemsSource="{Binding DroppedItems}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="MouseDoubleClick">
                <Custom:EventToCommand Command="{Binding DataContext.RenameItemCommand, ElementName=lw}"/>
            </i:EventTrigger>
         </i:Interaction.Triggers>

         <ListView.ItemTemplate >
            <DataTemplate >
                <Label Content="{Binding Field}" />
            </DataTemplate>
         </ListView.ItemTemplate>
</ListView>

你的命令:

private ICommand renameItemCommand;
    public ICommand RenameItemCommand
    {
        get
        {
            if (renameItemCommand == null)
            {
                renameItemCommand = new RelayCommand(
                                       param => RenameItem()
                                   );
            }
            return renameItemCommand;
        }
    }

private void RenameItem()
{

}

答案 1 :(得分:1)

以下代码适用于列表框,它应该是相同的:

<ListBox x:Name="listbox_name_here" 
    ItemsSource="{Binding LastEntries}"
    SelectedItem="{Binding SelectedExercise, UpdateSourceTrigger=PropertyChanged}"
    MinHeight="150" ToolTip="Double click to edit"
    >

  <i:Interaction.Triggers>
      <i:EventTrigger EventName="MouseDoubleClick">
         <Command:EventToCommand Command="{Binding your_command_name_here}" 
              CommandParameter="{Binding ElementName=listbox_name_here, 
                                         Path=SelectedItem}" />
      </i:EventTrigger>
  </i:Interaction.Triggers>

</ListBox>

请注意,命令参数使用列表框(在您的情况下为listview)名称来绑定所选项目的目标。

答案 2 :(得分:0)

一个选项可能是为ListView项创建一个DataTemplate,并在那里包含EventTrigger。例如,

<ListView x:Name="ItemsFromStash" 
          ItemsSource="{Binding DropBox.DroppedItems}" ItemTemplate="{DynamicResource DropItemTemplate}"
          SelectedItem="{Binding DropBox.SelectedDropItem}">
    <ListView.ItemTemplate>
        <DataTemplate>
            <Grid>
                <i:Interaction.Triggers>
                    <i:EventTrigger EventName="MouseLeftButtonUp">
                        <Custom:EventToCommand Command="{Binding DropBox.RenameItemCommand, Mode=OneWay}"/>
                    </i:EventTrigger>
                </i:Interaction.Triggers>
                <!-- Place your template controls here -->
            </Grid>                                        
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>