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?
Asked
Active
Viewed 847 times
3

Ondřej
- 1,645
- 1
- 18
- 29
-
4sure 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 Aug 17 '16 at 16:12
1 Answers
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.

Felipe Romero
- 179
- 3
- 9