Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Same method for multiple controls when using Caliburn.Micro

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?

like image 386
Ondřej Avatar asked Jul 12 '26 10:07

Ondřej


1 Answers

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.

like image 119
Felipe Romero Avatar answered Jul 16 '26 10:07

Felipe Romero



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!