3

In WPF MVVM application, I need same functionality for multiple controls - for example certain button does same thing as certain menu item. It is piece of cake with MVVM Light's RelayCommand, but I am now using Caliburn.Micro, where almost everything is based on conventions. So two controls can not have same x:Name="AddItem", which is used by CM to determine method for executing in ViewModel. Is there any simple way to solve this?

1
  • 4
    sure don't name them and bind what you need normally... use cal:Message.Attach="YourMethod()" to call the method on the viewmodel bound to the current Datacontext. ContextMenus can get tricky due to visualtree limitations.
    – mvermef
    Commented Aug 17, 2016 at 16:12

1 Answer 1

3

Yes, it's simple, but verbose. You need to use the "long format". Let's say you have one method IncrementCount on your ViewModel:

// Handling event
public void IncrementCount()
{
    Count++;
}

And your View has:

<Button Name="ButtonOne">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <cal:ActionMessage MethodName="IncrementCount" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

<Button Name="ButtonTwo">
    <i:Interaction.Triggers>
        <i:EventTrigger EventName="Click">
            <cal:ActionMessage MethodName="IncrementCount" />
        </i:EventTrigger>
    </i:Interaction.Triggers>
</Button>

Both buttons will call your IncrementCount method.

EDIT

Add these namespaces

xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:cal="http://www.caliburnproject.org"

You may see this Caliburn starting project using the snippets above.

Not the answer you're looking for? Browse other questions tagged or ask your own question.