Using Message.Attach in code instead of Xaml

Topics: Actions & Coroutines
Aug 25, 2013 at 1:37 PM
Typically, I create the View in xaml, and bind Events to the ViewModel using Caliburn.Micro.
<Button cal:Message.Attach="[MouseLeftButtonUp]=[ModifyList($source)]" />
However, I now need to create the button in code, based on configuration data.

The code is not in the CodeBehind, it's in a Factory class.
Button button = new Button(){Content = "Click Me"};
So the question is how to wire up the event in code instead of Xaml?

Aug 25, 2013 at 4:00 PM
I got a response on this over on StackOverflow.
var button = new Button { Content = "Click Me" };

var action = new ActionMessage();
action.MethodName = "ModifyList";
action.Parameters.Add(new Parameter { Value = DataContext });

var trigger = new System.Windows.Interactivity.EventTrigger();
trigger.EventName = "Click";
trigger.SourceObject = button;