简体   繁体   English

将ICommand绑定到自定义事件处理程序

[英]binding an ICommand to a custom eventhandler

I am trying to create a datapager usercontrol. 我正在尝试创建一个datapager usercontrol。 This user control has one event declared in its code behind : 此用户控件在其代码后面声明了一个事件:

public event EventHandler OnPageIndexPagerChanged;

I use this control in a main usercontrol, and I bind my event to another event in the code behind like this : 我在主用户​​控件中使用此控件,并将我的事件绑定到后面的代码中的另一个事件,如下所示:

xaml : xaml:

OnPageIndexPagerChanged="SuperDataPager_OnPageIndexPagerChanged"

c# (Code behind) : c#(代码背后):

private void SuperDataPager_OnPageIndexPagerChanged(object sender, EventArgs e)
        {
            var viewModel = DataContext as ViewSchedeConsuntiviViewModel;

            if (viewModel != null)
            {
                viewModel.FilterCommand.Execute(sender);
            }
        }

And this works fine, But I would have liked to skip passing through the code behind an do something like : 这工作正常,但我希望跳过传递代码背后的代码,如:

OnPageIndexPagerChanged="{Binding Path=FilterCommand}"

This, I already do for the filter button, present in my main usercontrol, therefore I thought I could do the same for this event. 这个,我已经为我的主用户控件中的过滤器按钮做了,因此我认为我可以为此事件做同样的事情。 But every time I receive an error : 但每次收到错误时:


Impossibile assegnare alla proprietà 'Super.Silverlight.SuperDataPager.OnPageIndexPagerChanged'. Impossibile assegnareallaproprietà'Super.Silverlight.SuperDataPager.OnPageIndexPagerChanged'。 [Line: 90 Position: 55] [线路:90位置:55]


'iexplore.exe' (Silverlight): Loaded 'C:\\Program Files (x86)\\Microsoft Silverlight\\4.0.60531.0\\it\\mscorlib.resources.dll' su System.Windows.Application.LoadComponent(Object component, Uri resourceLocator) su Super.Silverlight.SchedeConsuntivi.InitializeComponent() su Super.Silverlight.SchedeConsuntivi..ctor() 'iexplore.exe'(Silverlight):加载'C:\\ Program Files(x86)\\ Microsoft Silverlight \\ 4.0.60531.0 \\ it \\ mscorlib.resources.dll'su System.Windows.Application.LoadComponent(Object component,Uri resourceLocator) su Super.Silverlight.SchedeConsuntivi.InitializeComponent()su Super.Silverlight.SchedeConsuntivi..ctor()

Sorry for the error in Italian, but I was unable to get the exact translation -> (unable to assign property...) 对不起意大利语中的错误,但我无法得到确切的翻译 - >(无法分配属性...)

Could someone explain me this behavior? 有人能解释一下这种行为吗?

Thanks for reading, 谢谢阅读,

[EDIT] Here is the my view Model for the main usercontrol : [编辑]这是我的主要用户控件的视图模型:

 public class MyViewModel : DependencyObject, INotifyPropertyChanged
{
      public ICommand FilterCommand { get; set; }

      public MyViewModel()
      {
         FilterCommand = new Super.Silverlight.Tools.DelegateCommand<object>(p => Filter());
      }

    public void Filter()
    {
        //....blah blah blah...
    }
}

[/EDIT] [/编辑]

First of all your Event is not a RoutedEvent , something you should consider. 首先,你的活动不是RoutedEvent ,你应该考虑的事情。 But thats not the main problem. 但这不是主要问题。

Of course you can't set an ICommand to an EventHandler. 当然,您无法将ICommand设置为EventHandler。 Don't get confused of RoutedEvents with RoutedCommands. 不要将RoutedEvents与RoutedCommands混淆。 You have 2 options, you can give your UserControl an dependency Property of type ICommand and just fire that inside your code behind. 你有2个选项,你可以给你的UserControl一个ICommand类型的依赖属性,然后在你的代码中触发它。 In XAML you can bind now your command. 在XAML中,您现在可以绑定您的命令。 Or if you want to have that as an event, again consider using RoutedEvents, there are options to bind commands to routedevents like EventBehaviorFactory and some more. 或者,如果您希望将其作为事件,再次考虑使用RoutedEvents,可以选择将命令绑定到诸如EventBehaviorFactory等路由事件

The easiest solution would be to use an ICommand directly in you user control and remove the event. 最简单的解决方案是直接在用户控件中使用ICommand并删除事件。

I'm afraid you can not do this, cause Command is special type routed event, instead reading DataPager specification doesn't seems to support any commanding. 我恐怕你不能这样做,因为Command是特殊类型的路由事件,而不是阅读DataPager规范似乎不支持任何命令。 And what you do in code, instead, is assign to event. 而你在代码中所做的就是分配给事件。

EDIT 编辑

You can have also workarrounds but it will not definitely like you want here. 你也可以使用workarrounds,但它绝对不会像你想要的那样。

Regards. 问候。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM