【问题标题】:How to handle a right click on a ListViewItem when ListView has an ItemTemplate?当 ListView 有 ItemTemplate 时如何处理 ListViewItem 的右键单击?
【发布时间】:2016-12-22 22:51:02
【问题描述】:

我有一个绑定到 ViewModel 的 ListView。 我想选择右键单击的 ListViewItem 以及触发更改 ViewModel 属性的触发器。(选择模式为单一)。

<ListView MinHeight="50" MaxHeight="120" 
ItemsSource="{Binding Path=DisplayItems}" SelectedItem="{Binding Path=SelectedDisplayItem, Mode=TwoWay}"
ItemTemplate="{StaticResource SelectedDisplayItemTemplate}" 
IsItemClickEnabled="False"
helpers:AttachedCommand.Command="{Binding Path=Commands[NavigateToDisplayItem]}"
helpers:AttachedCommand.Event="Tapped" />

我已尝试处理 ListView 中的 Right Tapped 事件,但无法获取被点击的项目。如何在右键单击时更改所选项目,并在选中时将 SelectedDisplayItem 设置为右键单击项目?

【问题讨论】:

  • 既然您将ItemTemplate 设置为SelectedDisplayItemTemplate,当您在那里设置RightTapped 事件时会发生什么?
  • 好吧,因为它是一个模板并从样式文件中获取其内容,所以我不想在那里放置 RightTapped。
  • 也许您可以将您的 itemtemplate 设为用户控件并在其中处理 RightTapped。

标签: c# xaml windows-runtime uwp


【解决方案1】:

尝试使用行为来实现

<ListView
    x:Name MainListView
    MinHeight="50" 
    MaxHeight="120" 
    ItemsSource="{Binding DisplayItems}" 
    SelectedItem="{Binding SelectedDisplayItem, Mode=TwoWay}"
    ItemTemplate="{StaticResource SelectedDisplayItemTemplate}">
    <i:Interaction.Behaviors>
         <core:EventTriggerBehavior
             EventName="RightTapped">
             <core:InvokeCommandAction
                  Command="{Binding YourCommand}"
                  CommandParameter="{Binding ElementName=MainListView, Path=SelectedItem}"/>
            </core:EventTriggerBehavior>
   </i:Interaction.Behaviors>
</ListView>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-05-30
    • 1970-01-01
    • 2014-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-07-04
    • 1970-01-01
    相关资源
    最近更新 更多