Vegetico
Vegetico

Reputation: 123

MVVM Light, Mahapps metro and EventToCommand via EventTrigger from System.Windows.Interactivity

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

Upvotes: 0

Views: 346

Answers (1)

punker76
punker76

Reputation: 14611

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>

Upvotes: 0

Related Questions