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

Having trouble using new EventTrigger with InvokeCommandAction (Managed Noesis 2.2.3)

11 Jul 2019, 19:26

I'm trying to connect the PreviewMouseDown event from my View to a DelegateCommand in my ViewModel. My View's DataContext is properly set to my ViewModel and binding is working fine.

View.xaml
<UserControl x:Class="Modules.FileManager.FileManagerView"
             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
             xmlns:local="clr-namespace:Modules.FileManager"
             xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity" 
             Initialized="OnInitialized">
             
        <ListView
                SelectionChanged="OnSelectionChanged"
                SelectionMode="Single"
                Style="{StaticResource FileManagerListViewStyle}"
                ItemContainerStyle="{StaticResource FileManagerListViewItemStyle}"                    
                ItemsSource="{Binding FileManagerCollectionView.FileManagerCollectionView}">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="PreviewMouseDown">
                    <i:InvokeCommandAction Command="{Binding ListViewDownCommand}"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
            <ListView.ItemTemplate>
            ...


ViewModel
        ...
        public FileManagerViewModel(IFileManagerCollectionView collectionView)
        {
            FileManagerCollectionView = collectionView;

            ListViewDownCommand = new DelegateCommand(OnListViewDownCommand);
        }
        private void OnListViewDownCommand(object obj)
        {
            throw new NotImplementedException();
        }
        public ICommand ListViewDownCommand { get; private set; }
        ...
I stole the DelegateCommand from your latest samples and ICommand from windows.
I'm not seeing the command executed. It just fails silently.

Is there something I'm missing here?

Tags:
 
User avatar
sfernandez
Site Admin
Posts: 1683
Joined: 22 Dec 2011, 19:20

Re: Having trouble using new EventTrigger with InvokeCommandAction (Managed Noesis 2.2.3)

12 Jul 2019, 12:44

I don't see in your code what could be wrong, so I created a similar test trying to replicate what you are doing and it works as expected.
Please see attached project and let me know if you find anything different in your environment.
Attachments
EventTriggerTest.zip
(3.67 KiB) Downloaded 9 times
 
darthmaule2
Topic Author
Posts: 89
Joined: 23 Oct 2014, 19:54

Re: Having trouble using new EventTrigger with InvokeCommandAction (Managed Noesis 2.2.3)

12 Jul 2019, 21:30

Short story:
Thanks for the minimal working example - it helped me figure this out - and I now I see what I did wrong...

Long story:
When I had previously ported some Prism classes, I saw ICommand was required, but that it was in System.Windows.Input. Thinking (incorrectly, apparently) it was a Windows only assembly used for WPF, I just created a new local file for the interface. At the time I didn't realize it had some attributes attached to it and left them out, so I just had a broken interface ... :(

In other words, I had everything below but the using statements and the attributes in a local file called ICommand.
using System.ComponentModel;
using System.Windows.Markup;

namespace System.Windows.Input
{
    //
    // Summary:
    //     Defines a command.
    [TypeConverter("System.Windows.Input.CommandConverter, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
    [ValueSerializer("System.Windows.Input.CommandValueSerializer, PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, Custom=null")]
    public interface ICommand
    {
        //
        // Summary:
        //     Occurs when changes occur that affect whether or not the command should execute.
        event EventHandler CanExecuteChanged;

        //
        // Summary:
        //     Defines the method that determines whether the command can execute in its current
        //     state.
        //
        // Parameters:
        //   parameter:
        //     Data used by the command. If the command does not require data to be passed,
        //     this object can be set to null.
        //
        // Returns:
        //     true if this command can be executed; otherwise, false.
        bool CanExecute(object parameter);
        //
        // Summary:
        //     Defines the method to be called when the command is invoked.
        //
        // Parameters:
        //   parameter:
        //     Data used by the command. If the command does not require data to be passed,
        //     this object can be set to null.
        void Execute(object parameter);
    }
}

Anyway, I reverted to using the ICommand from System.Windows.Input and it works in both Windows & Linux!
 
darthmaule2
Topic Author
Posts: 89
Joined: 23 Oct 2014, 19:54

Re: Having trouble using new EventTrigger with InvokeCommandAction (Managed Noesis 2.2.3)

12 Jul 2019, 21:39

I do have another question about this scenario -- why is the param = null? I would except it would contain the source object so I can know what triggered the command? This happens in your minimal example.
 
User avatar
sfernandez
Site Admin
Posts: 1683
Joined: 22 Dec 2011, 19:20

Re: Having trouble using new EventTrigger with InvokeCommandAction (Managed Noesis 2.2.3)

16 Jul 2019, 11:59

Great to know you found it.

InvokeCommandAction also defines a CommandParameter property that is used as source for the ICommand parameter. You can use that property to store whatever you want: a resource, a bound value, a string...

Who is online

Users browsing this forum: No registered users and 2 guests