【问题标题】:Binding to a method wpf绑定到方法 wpf
【发布时间】:2011-08-16 00:59:24
【问题描述】:

我的 xaml 视图文件中有 <TabControl AllowDrop="True" PreviewDragOver="{Binding Path=DragOver}" PreviewDrop="Drop">

DragOver 函数位于 ViewModel.cs 类中。

我收到以下错误

PreviewDragOver="{Binding Path=DragOver}" is not valid. '{Binding Path=DragOver}' is not a valid event handler method name. Only instance methods on the generated or code-behind class are valid. 

我尝试绑定到函数的方式有什么问题? 应该怎么样?

【问题讨论】:

标签: .net wpf data-binding mvvm binding


【解决方案1】:

您只能绑定到您的 viemmodel 中的属性,如果您需要从您的 viemodel 调用任何函数,您需要通过路由命令来完成。要开始如何实现命令,请查看这些链接,它们可能会有所帮助 -

http://www.codeproject.com/KB/WPF/VMCommanding.aspx (1) http://msdn.microsoft.com/en-us/library/ms752308.aspx(2)

【讨论】:

  • 但是如何将 DragEventArgs 从视图传递到 Relay 命令?
【解决方案2】:

PreviewDragDrop 是一个与PreviewDrop 类似的事件,它希望获得一个适当的事件处理程序。如果DragOver 是事件处理程序,那么您的代码应该类似于<TabControl AllowDrop="True" PreviewDragOver="DragOver" PreviewDrop="Drop">

【讨论】:

  • 当我在 xaml 后面的代码中有这个事件处理程序时我没有问题,但是我移动到 viewmodel 类我得到这个错误
猜你喜欢
  • 2012-12-06
  • 2010-10-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-08-23
  • 2013-08-31
  • 2011-02-02
  • 1970-01-01
相关资源
最近更新 更多