0
votes

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?

1
Try something like this <i:EventTrigger EventName="{x:Static behaviours:ChartBehavior.OnVisualChartRangeChanged"> for your i:EventTrigger.Suresh
Can you be more specific? I did this and threw an exception:guerrillacodester
instead of this <i:EventTrigger EventName="OnVisualChartRangeChanged">, try code from my prev. comment. And, what was the exception?Suresh
I have eddited my previous comment to show you what I did and the error that I got.guerrillacodester

1 Answers

0
votes

I've already posted an answer to your other identical question. Notice how the event name is specified as a string in the link and NOT using x:Static.

Here's your other question How to handle a custom RoutedEvent fired from a UserControl from inside the ViewModel?

And here's the link Custom RoutedEvent as EventTrigger

So do this :

<EventTrigger RoutedEvent="behaviours:ChartBehavior.OnVisualChartRangeChanged">

Finally i want to add that there is nothing wrong with calling your ViewModel handler from inside the code behind handler! Keep it simple.