【问题标题】:ListViewItem MouseDoubleClick MVVM wayListViewItem MouseDoubleClick MVVM方式
【发布时间】:2013-11-04 01:02:23
【问题描述】:

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

应该怎么做?

【问题讨论】:

  • 如果识别项目对您很重要,您可以创建一个行为,在其中添加一个处理程序以将单击事件添加到列表视图,并且 e.OriginalSource 应该是列表视图项目
  • 你知道为什么它们有时为空吗?你能重复这种行为吗?此外,对于将来的参考,下一点在问题中是相当无用的,因为它没有添加任何内容,只是让人们阅读他们不需要的代码:Grid.Column="0" Grid.Row="1" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"
  • @Noctis 不幸的是我不能每次都重复。这似乎是随机的。

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


【解决方案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】:

查看:

 <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()
{

}

【讨论】:

  • 我试过解决这个问题请看我的回答,我改了。我检查了一下,一切正常)))
【解决方案3】:

一种选择可能是为 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>

【讨论】:

    猜你喜欢
    • 2021-11-25
    • 2015-11-29
    • 2018-08-04
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 1970-01-01
    • 2016-01-20
    • 1970-01-01
    相关资源
    最近更新 更多