I have a UserControl with a registered routed event:
public static readonly RoutedEvent OnVisualChartRangeChangedEvent =
EventManager.RegisterRoutedEvent("OnVisualChartRangeChanged",
RoutingStrategy.Bubble, typeof(OnScrollChangedHandler), typeof(ChartBehavior));
public event OnScrollChangedHandler OnVisualChartRangeChanged
{
add { AssociatedObject.AddHandler(OnVisualChartRangeChangedEvent, value); }
remove { AssociatedObject.RemoveHandler(OnVisualChartRangeChangedEvent, value); }
}
I fire this routed event from the user control like this:
protected virtual void OnScrollEvent(object oldValue, object newValue)
{
AssociatedObject.RaiseEvent(new DateTimeEventArgs(OnVisualChartRangeChangedEvent, minDate, maxDate));
}
I imbed this usercontrol inside my main view like this:
Title="MainWindow" Height="350" Width="525">
<Grid>
<historicChart:HistoricChartControl></historicChart:HistoricChartControl>
</Grid>
and I would like to handle the custom routed event by calling a method from my Viewmodel when this event is fired.
I tried this at first:
<Grid>
<historicChart:HistoricChartControl behaviours:ChartBehavior.OnVisualChartRangeChanged="VisualChartRangeChanged"/>
</Grid>
but this requires that I go against the MVVM since the handler method (VisualChartRangeChanged) would be placed in the View and not the Viewmodel.
I then tried this:
Title="MainWindow" Height="350" Width="525">
<Grid>
<!--behaviours:ChartBehavior.OnVisualChartRangeChanged="RoutedEventHandler"-->
<historicChart:HistoricChartControl>
<i:Interaction.Triggers>
<i:EventTrigger EventName="OnVisualChartRangeChanged">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="MyMethod"/>
</i:EventTrigger>
</i:Interaction.Triggers>
</historicChart:HistoricChartControl>
</Grid>
but the event handler method (MyMethod) is not being called.
Can someone please provide an example for how this is done?
Thanks.
EDITED
So I modified the code as follows:
<i:Interaction.Triggers>
<i:EventTrigger EventName="{x:Static behaviours:ChartBehavior.OnVisualChartRangeChangedEvent}">
<ei:CallMethodAction TargetObject="{Binding}" MethodName="MyMethod"/>
</i:EventTrigger>
</i:Interaction.Triggers>
Not only do I see this: http://i.stack.imgur.com/xR9DY.jpg
but even though it compiles despite the notification shown, I get this inner exception:
{"'ChartBehavior.OnVisualChartRangeChanged' is not a valid value for property 'EventName'."}
Is there a better technique to handle a routed event from a usercontrol by calling a handler method in the main view's viewmodel?
<i:EventTrigger EventName="{x:Static behaviours:ChartBehavior.OnVisualChartRangeChanged">
for youri:EventTrigger
. – Suresh<i:EventTrigger EventName="OnVisualChartRangeChanged">
, try code from my prev. comment. And, what was the exception? – Suresh