0

I'm going nuts on this one: I have an application using both Mahapps.Metro as well as MVVMLight. Basically most things are ok UNTIL you try to use Interaction.Triggers.

I typically end up with errors like

Cannot add instance of type 'EventToCommand' to a collection of type 'TriggerActionCollection'. Only items of type 'T' are allowed.

A way to reproduce this is via a repo I found online: https://github.com/mike-schulze/mahapps-mvvmlight-setup (sorry mike for hijacking) and adding a metro SplitButton to the viewmodel:

<Controls:SplitButton ItemsSource="{Binding MyList}" Grid.Column="1" Grid.Row="1">
            <i:Interaction.Triggers>
                <i:EventTrigger EventName="SelectionChanged">
                    <cmd:EventToCommand 
                                    Command="{Binding Mode=TwoWay, Path=SelectionChangedCommand}" 
                                    PassEventArgsToCommand="True"/>
                </i:EventTrigger>
            </i:Interaction.Triggers>
        </Controls:SplitButton>

And MyList being

        public List<string> MyList
        {
            get
            {
                List<string> theList = new List<string>();
                theList.Add("Hello");
                theList.Add("World");
                return theList;
            }
        }

I'm not sure how many different namespaces, hard coded Interactivity versions in App.config or what so ever I tried.

Has anyone a working example with Mahapps Metro (version not too old) and MVVM Light and the EventToCommand stuff?

I thought my problem is related to updates to Metro - and them using Behaviors now instead of Interactivity. I'm using the Interactivity dll version 4.5.0.0. But even the old git I mentioned above shows the problem... And I can't find a working solution.

Any help is greatly appreciated. Thanks

Vegetico
  • 93
  • 8

1 Answers1

0

You need to add the namespace for System.Windows.Interactivity too.

xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"

Because MVVMLight uses this version of Interactivity.

<mah:MetroWindow x:Class="..."
                 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"

                 ...

                 xmlns:mah="http://metro.mahapps.com/winfx/xaml/controls"
                 xmlns:i="http://schemas.microsoft.com/expression/2010/interactivity"
                 xmlns:command="http://www.galasoft.ch/mvvmlight"

                 ...

                 DataContext="{Binding Main, Source={StaticResource Locator}}">

    ...
    
    <mah:SplitButton ItemsSource="{Binding MyList}">
        <i:Interaction.Triggers>
            <i:EventTrigger EventName="SelectionChanged">
                <command:EventToCommand Command="{Binding Mode=TwoWay, Path=SelectionChangedCommand}" PassEventArgsToCommand="True" />
            </i:EventTrigger>
        </i:Interaction.Triggers>
    </mah:SplitButton>

    ...

</mah:MetroWindow>
punker76
  • 14,326
  • 5
  • 58
  • 96
  • Hi, I assume you are 'the' mahapps punker76 :) Nice to meet you. I also used the interactivity namespace while trying to solve the problem - but without much success. So following that approach results in a ```The type 'EventToCommand' from assembly 'GalaSoft.MvvmLight.Platform' is built with an older version of the Blend SDK, and is not supported in a Windows Presentation Framework 4 project```. What I get here https://stackoverflow.com/questions/8360209/how-to-add-system-windows-interactivity-to-project I guess I have some conflicts with behavior vs interactivity. – Vegetico Jul 31 '20 at 07:17
  • @Vegetico This 2 libs are different dll's with different namespaces, so it can work together. My answered post is from a working sample. – punker76 Jul 31 '20 at 09:12
  • Can you share the project? My other thought is that its related to the .NET framework - based on the link above. My xaml looks identical to yours - so I guess its somewhere between .NET version and lib version of the used components. – Vegetico Jul 31 '20 at 09:54