本文介绍了如何处理从ViewModel内部的UserControl触发的自定义RoutedEvent?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前,我有一个用户控件可以触发注册的RoutedEvent,如下所示:

Currently I have a usercontrol that fires a registered RoutedEvent like this:

    protected virtual void OnScrollEvent(object oldValue, object newValue)
    {
        AssociatedObject.RaiseEvent(new DateTimeEventArgs(OnVisualChartRangeChangedEvent, minDate, maxDate));
    }

mainwindow容器目前通过从这样的视图调用方法来处理这个事件。

The mainwindow container currently handles this event by calling a method from the view like this.

<Grid>
    <historicChart:HistoricChartControl behaviours:ChartBehavior.OnVisualChartRangeChanged="RoutedEventHandler"/>
</Grid>

和代码背后...

    private void RoutedEventHandler(object sender, DateTimeEventArgs dateTimeEventArgs)
    {
               //do stuff here...
    }

但是我想要的是符合MVVM模型,这样我的处理程序应该实现viewmodel,而不是在视图中。

But what I would like is for this to conform to the MVVM model, so that my handler should be implemented the viewmodel and not in the view.

我该怎么做?有人可以给我发一个简短的例子,说明我可以这么做吗?

How can I do this? Could someone post me a brief example of how I could go about this?

感谢在advnce

推荐答案

您可以使用调用您的ViewModel处理程序。

You can use the CallMethodAction to invoke your ViewModel handler.

请参阅如何为自定义RoutedEvent创建EventTrigger:

See this on how to create an EventTrigger for your custom RoutedEvent :Custom RoutedEvent as EventTrigger

这篇关于如何处理从ViewModel内部的UserControl触发的自定义RoutedEvent?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-13 17:06