I'm trying to use caliburn micro message to trigger an attached event that I created:
public static class DataChanging
{
public delegate void DataChangingEventHandler(object sender, DataChangingEventArgs e);
public static readonly RoutedEvent ChangingEvent =
EventManager.RegisterRoutedEvent("Changing",
RoutingStrategy.Bubble,
typeof(DataChangingEventHandler),
typeof(DataChanging));
public static void AddChangingHandler(DependencyObject o, DataChangingEventHandler handler)
{
((UIElement)o).AddHandler(DataChanging.ChangingEvent, handler);
}
public static void RemoveChangingHandler(DependencyObject o, DataChangingEventHandler handler)
{
((UIElement)o).RemoveHandler(DataChanging.ChangingEvent, handler);
}
public static bool GetActivationMode(DependencyObject obj)
{
return (bool)obj.GetValue(ActivationModeProperty);
}
public static void SetActivationMode(DependencyObject obj, bool value)
{
obj.SetValue(ActivationModeProperty, value);
}
public static readonly DependencyProperty ActivationModeProperty =
DependencyProperty.RegisterAttached("ActivationMode",
typeof(bool),
typeof(DataChanging),
new FrameworkPropertyMetadata(false,
HandleActivationModeChanged));
private static void HandleActivationModeChanged(DependencyObject target, DependencyPropertyChangedEventArgs e)
{
var dataGrid = target as XamDataGrid;
if (dataGrid == null) // if trying to attach to something else than a datagrid, just ignore
return;
if ((bool)e.NewValue)
{
dataGrid.RecordDeactivating += selector_RecordDeactivating;
}
else
{
dataGrid.RecordDeactivating -= selector_RecordDeactivating;
}
}
static void selector_RecordDeactivating(object sender, RecordDeactivatingEventArgs e)
{
var args = new DataChangingEventArgs(DataChanging.ChangingEvent,sender)
{
Data = ((DataRecord) e.Record).DataItem,
ShouldCancelChange = false
};
(sender as UIElement).RaiseEvent(args);
e.Cancel = args.ShouldCancelChange;
}
}
In the XAML itself I added the following line:
cal:Message.Attach="[Helpers:DataChanging.Changing] = [Action SelectedDataChanged($eventArgs)]"
Helpers refer to the right namespace. I also tried other versions that failed (full namespace):
cal:Message.Attach="[clr-namespace:RTF.Client.UI.Helpers.DataChanging.Changing] = [Action SelectedDataChanged($eventArgs)]"
tried to set the interaction event by myself:
When I tried adding a normal event trigger everything worked well, so it's not my attached event declaration :
<EventTrigger RoutedEvent="Helpers:DataChanging.Changing">
<EventTrigger.Actions>
<BeginStoryboard x:Name="sb">
<Storyboard x:Name="dsf">
<Storyboard x:Name="myStoryboard">
<BooleanAnimationUsingKeyFrames Storyboard.TargetName="SSS" Storyboard.TargetProperty="IsChecked">
<DiscreteBooleanKeyFrame KeyTime="00:00:00" Value="False" />
</BooleanAnimationUsingKeyFrames>
</Storyboard>
</Storyboard>
</BeginStoryboard>
</EventTrigger.Actions>
</EventTrigger>
What am I doing wrong here? There is no way to attach an attached event and invoke it using caliburn micro?
I dont think that the parser for the shortened Message.Attach syntax supports attached events. But why don't you just add the ActionMessage directly to the Actions of the EventTrigger?
finally I understand the problem and the solution. The problem is that system.windows.interactiviy.EventTrigger doesn't support attached events. Caliburn micro uses it for actions thus my attached event didn't work. The solution was to write a customized event trigger and use caliburn micro action in a explicit manner. the customized event trigger was taken from that post:http://joyfulwpf.blogspot.com/2009/05/mvvm-invoking-command-on-attached-event.html?showComment=1323674885597#c8041424175408473805
and then when you want to use caliburn action:
Have you tried this?
I needed to send an event from a child control to the parents ViewModel, and it worked fine for me. I'll post some example code maybe it'll help someone out!
Child Control codebehind:
Child control XAML:
Parent view: