gpt4 book ai didi

mvvm - 在 WPF MVVM 中手动更新数据绑定(bind)

转载 作者:行者123 更新时间:2023-12-01 05:12:46 26 4
gpt4 key购买 nike

我的 View 模型:

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

在我的 View我将标签的内容绑定(bind)到 ViewModel 的 FileName .

现在当我将文件拖放到我的 View 时,如何更新标签的 Content属性,使 ViewMode 的 FileName也通过绑定(bind)更新?

直接设置标签的 Content属性不起作用,它只是简单地清除绑定(bind)。

最佳答案

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

  • 您可以简单地将 VM 从 View 的 DataContext 中拉出,并在拖放事件期间设置 ViewModel 的 FileName 属性。
  • 使用 AttachedBehavior 允许事件(拖放)像命令一样使用(http://geekswithblogs.net/HouseOfBilz/archive/2009/08/27/adventures-in-mvvm-ndash-binding-commands-to-any-event.aspx)
  • 使用 Messenger 模式,如 MVVMLight 的 Messenger,将消息从 View 发送到 ViewModel,并像处理命令操作一样在 VM 上处理消息。
  • 关于mvvm - 在 WPF MVVM 中手动更新数据绑定(bind),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2867351/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com