darthmaule2
Topic Author
Posts: 98
Joined: 23 Oct 2014, 19:54

Any way to attach a command to the PreviewMouseDown event of a ListViewItem?

05 Aug 2019, 21:42

I have a <ListView> which I can attach a command to the PreviewMouseDown event, and that works well.

But, because it doesn't provide the EventArgs, I can't see which ListViewItem was clicked.

Is there a way to attach this same event but to the ListViewItem itself? I've tried putting it into the ControlTemplate and the Style but haven't been able to get either working.
        <ListView
                SelectionMode="Single"
                Style="{StaticResource FileManagerListViewStyle}"
                ItemContainerStyle="{StaticResource FileManagerListViewItemStyle}"                    
                ItemsSource="{Binding FileManagerCollectionView.FileManagerCollectionView}">
            <i:Interaction.Triggers>
                <i:EventTrigger RoutedEvent="PreviewMouseDown">
                    <i:InvokeCommandAction Command="{Binding ListViewDownCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            ...
I'm not sure how to do this in WPF either, but I've seen some work-around which I don't think can be done in Noesis.

Thanks!
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Any way to attach a command to the PreviewMouseDown event of a ListViewItem?

07 Aug 2019, 12:52

We implemented an extension in Noesis to define interactivity triggers/behaviors in styles, and you can use that in ListViewItem style like this:
<Grid
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
    xmlns:noesis="clr-namespace:NoesisGUIExtensions;assembly=Noesis.GUI.Extensions">
      
  <Grid.Resources>
    <Style x:Key="FileManagerListViewItemStyle" TargetType="ListViewItem">
      ...
      <Setter Property="noesis:StyleInteraction.Triggers">
        <Setter.Value>
          <noesis:StyleTriggerCollection>
            <i:EventTrigger EventName="PreviewMouseDown">
              <i:InvokeCommandAction
                Command="{Binding DataContext.ListViewDownCommand, RelativeSource={RelativeSource AncestorType={x:Type ListView}}}"
                CommandParameter="{Binding}"/>
            </i:EventTrigger>
          </noesis:StyleTriggerCollection>
        </Setter.Value>
      </Setter>
    </Style>
  </Grid.Resources>
  ...
</Grid>
This way you will receive in the command parameter the item associated with the mouse down event. Does this work for you?
 
darthmaule2
Topic Author
Posts: 98
Joined: 23 Oct 2014, 19:54

Re: Any way to attach a command to the PreviewMouseDown event of a ListViewItem?

07 Aug 2019, 15:50

That is fantastic!... but, it looks like Noesis.GUI.Extensions requires some blend DLLs which are full .NET only right now?

Looks like Microsoft open sourced those libraries here:
https://devblogs.microsoft.com/dotnet/o ... s-for-wpf/

And this unmerged pull request adds .NET core 3.0 support:
https://github.com/microsoft/XamlBehaviorsWpf/pull/20

Think that will be cross-platform when merged or just be .NET core 3.0 on Windows only?
 
User avatar
sfernandez
Site Admin
Posts: 2991
Joined: 22 Dec 2011, 19:20

Re: Any way to attach a command to the PreviewMouseDown event of a ListViewItem?

07 Aug 2019, 16:56

Noesis.GUI.Extensions NuGet is only required for WPF Blend editing.
Those classes are already exposed in the cross-platform Noesis.App NuGet, so it will correctly work in your .NET Core 3.0 project.
 
darthmaule2
Topic Author
Posts: 98
Joined: 23 Oct 2014, 19:54

Re: Any way to attach a command to the PreviewMouseDown event of a ListViewItem?

07 Aug 2019, 20:53

Understood, thank you. I have a dozen places in the code where this would be very helpful.

Who is online

Users browsing this forum: Google [Bot] and 12 guests