【问题标题】:Handling wpf control events in the ViewModel在 ViewModel 中处理 wpf 控件事件
【发布时间】:2010-12-26 10:51:11
【问题描述】:

是否有替代附加命令行为来处理诸如 DataGrid 的 MouseDoubleClick 事件或 ViewModel 中 TextBox 的 LostFocus 事件之类的事件?

例如,查看::

    <Window ........
            DataContext=".......">


        <TextBox LostFocus="{Binding Command1}" />

        <! -- or -->

        <WpfToolkit:DataGrid MouseDoubleClick="{Binding Command2}".../>

    </Window>

ViewModel::

public class MyViewModel
{
   public ICommand Command1
   {
      .......
   }


   public ICommand Command2
   {
      .......
   }
}

【问题讨论】:

  • 为什么不想使用附加的命令行为?

标签: wpf mvvm event-handling


【解决方案1】:

我创建了一个 BindEvent 类,我在视图中使用它来将事件绑定到数据上下文中的方法。我是这样使用的:

public MyView()
{
  InitializeComponent();
  Loaded += new RoutedEventHandler(BindEvent.Create("ControlLoaded"));
}

如果你有兴趣,我可以稍后在这里添加源代码。

【讨论】:

  • 实际上,我想在没有任何代码的情况下实现这一点。我想我毕竟必须选择 ACB。不过谢谢你的回复。
猜你喜欢
  • 2013-08-05
  • 2012-06-27
  • 1970-01-01
  • 2015-02-02
  • 1970-01-01
  • 2017-03-24
  • 1970-01-01
  • 2017-07-01
  • 2011-01-20
相关资源
最近更新 更多