Greg
Greg

Reputation: 10798

How can I get an event or command to fire when the user clicks on a ListViewItem?

I have a list in my application's UI and I'd like to open a popover when the user clicks on an item in the list. As far as I can tell, there are no Click events on ListViewItem that will help me with this. The SelectionChanged event will not work either, since the user can close the popover and I'd like to be able to open it again without requiring them to select a different item in the list first.

This is what my ListView creation code looks like, for what it's worth:

<ListView x:Name="_resultsListView"
          ItemsSource="{Binding AvailableResults}"
          SelectedItem="{Binding SelectedResult}"
          ItemContainerStyle="{StaticResource LoadResultItemStyle}"
          VirtualizingStackPanel.IsVirtualizing="True"
          Style="{DynamicResource DefaultListViewStyle}" BorderThickness="0"/>

Thanks! Any suggestions would be appreciated.

Upvotes: 1

Views: 377

Answers (2)

Rachel
Rachel

Reputation: 132618

What HB suggested works, although I find a more permanent solution for binding Events to Commands is to use an AttachedCommandBehavior that will let you attach a command to just about any event

<Style TargetType="{x:Type ListViewItem}">
    <Setter Property="local:CommandBehavior.Event" Value="Click" />
    <Setter Property="local:CommandBehavior.Command" Value="{Binding DataContext.MyCommand, ElementName=MyListView}" />
    <Setter Property="local:CommandBehavior.CommandParameter" Value="{Binding }" />
</Style>

Upvotes: 1

brunnerh
brunnerh

Reputation: 185300

Put a Button in the ItemTemplate (or ListViewItem.Template set via the ItemContainerStyle) and bind it to a command, you can style it to be invisible if need be.

Upvotes: 0

Related Questions