【问题标题】:Data binding manually update in WPF MVVM在 WPF MVVM 中手动更新数据绑定
【发布时间】:2010-05-19 16:13:12
【问题描述】:

我的视图模型:

class ViewModel
{
public string FileName {get;set;}
}

在我的 View 中,我将标签的内容绑定到 ViewModel 的 FileName

现在当我将文件拖放到我的视图时,如何更新标签的 Content 属性,以便 ViewMode 的 FileName 也通过绑定得到更新?

直接设置标签的Content属性是不行的,只是清除绑定而已。

【问题讨论】:

    标签: mvvm drag-and-drop binding


    【解决方案1】:

    3 个快速选择...(确保类实现了 INotifyPropertyChanged,并且 FileName 正在引发此事件。)

    1. 您可以简单地将 VM 从 View 的 DataContext 中拉出,并在拖放事件期间设置 ViewModel 的 FileName 属性。

    2. 使用 AttachedBehavior 允许事件(拖放)像命令一样使用 (http://geekswithblogs.net/HouseOfBilz/archive/2009/08/27/adventures-in-mvvm-ndash-binding-commands-to-any-event.aspx)

    3. 使用 Messenger 模式(如 MVVMLight 的 Messenger)将消息从 View 发送到 ViewModel,并像处理命令操作一样在 VM 上处理消息。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-24
      • 1970-01-01
      • 1970-01-01
      • 2017-08-20
      • 1970-01-01
      • 2016-12-17
      • 2019-06-23
      相关资源
      最近更新 更多