2

我正在将我的项目转换为使用 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>

但它说:

属性“触发器”不可附加到“样式”类型的元素

我尝试将命令移动到 ListView.MouseDoubleClick,但有时 SelectedItem 为空。

应该怎么做?

4

3 回答 3

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>

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

于 2013-11-04T10:35:41.917 回答
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()
{

}
于 2013-11-04T09:46:00.470 回答
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>
于 2013-11-04T03:18:16.620 回答