Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I use CallMethodAction to call a method inside the viewmodel when a custom routed event is fired from inside the usercontrol?
    primarykey
    data
    text
    <p>I have a UserControl with a registered routed event: </p> <pre><code> 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); } } </code></pre> <p>I fire this routed event from the user control like this:</p> <pre><code> protected virtual void OnScrollEvent(object oldValue, object newValue) { AssociatedObject.RaiseEvent(new DateTimeEventArgs(OnVisualChartRangeChangedEvent, minDate, maxDate)); } </code></pre> <p>I imbed this usercontrol inside my main view like this:</p> <pre><code> Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;historicChart:HistoricChartControl&gt;&lt;/historicChart:HistoricChartControl&gt; &lt;/Grid&gt; </code></pre> <p>and I would like to handle the custom routed event by calling a method from my Viewmodel when this event is fired.</p> <p>I tried this at first: </p> <pre><code>&lt;Grid&gt; &lt;historicChart:HistoricChartControl behaviours:ChartBehavior.OnVisualChartRangeChanged="VisualChartRangeChanged"/&gt; &lt;/Grid&gt; </code></pre> <p>but this requires that I go against the MVVM since the handler method (VisualChartRangeChanged) would be placed in the View and not the Viewmodel.</p> <p>I then tried this:</p> <pre><code> Title="MainWindow" Height="350" Width="525"&gt; &lt;Grid&gt; &lt;!--behaviours:ChartBehavior.OnVisualChartRangeChanged="RoutedEventHandler"--&gt; &lt;historicChart:HistoricChartControl&gt; &lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="OnVisualChartRangeChanged"&gt; &lt;ei:CallMethodAction TargetObject="{Binding}" MethodName="MyMethod"/&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; &lt;/historicChart:HistoricChartControl&gt; &lt;/Grid&gt; </code></pre> <p>but the event handler method (MyMethod) is not being called.</p> <p>Can someone please provide an example for how this is done?</p> <p>Thanks.</p> <hr> <p>EDITED</p> <p>So I modified the code as follows:</p> <pre><code> &lt;i:Interaction.Triggers&gt; &lt;i:EventTrigger EventName="{x:Static behaviours:ChartBehavior.OnVisualChartRangeChangedEvent}"&gt; &lt;ei:CallMethodAction TargetObject="{Binding}" MethodName="MyMethod"/&gt; &lt;/i:EventTrigger&gt; &lt;/i:Interaction.Triggers&gt; </code></pre> <p>Not only do I see this: <a href="http://i.stack.imgur.com/xR9DY.jpg" rel="nofollow">http://i.stack.imgur.com/xR9DY.jpg</a></p> <p>but even though it compiles despite the notification shown, I get this inner exception:</p> <p>{"'ChartBehavior.OnVisualChartRangeChanged' is not a valid value for property 'EventName'."}</p> <p>Is there a better technique to handle a routed event from a usercontrol by calling a handler method in the main view's viewmodel?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload