【问题标题】:WPF - Binding and Command on two different ViewModelWPF - 两个不同 ViewModel 上的绑定和命令
【发布时间】:2016-05-03 08:44:27
【问题描述】:

它是这样的。我在同一个 XAML 文件下有两个视图模型。

ViewModel(1) - 包含一个按钮,该按钮具有将触发该视图模型内的函数的命令。

ViewModel(2) - 包含绑定到 XAML 的属性

问题:我是否可以使用 命令更新 ViewModel(2) 中的特定 属性 > 在 ViewModel(1) 中?如果是,我能否提供一个关于如何处理它的简要指南?

要求让我不要进行任何更改,例如将 property 移动到与 command 相同的视图模型中。

提前感谢您的任何帮助:)

【问题讨论】:

  • 您不能简单地从您的 ViewModel(1) 中引用 ViewModel(2),然后在调用命令时更改属性吗?
  • 在视图模型 1 中,您可以存储更新视图模型 2 中属性的操作。在视图模型 1 中,您不能调用该操作并且视图模型 2 已更改。
  • @3615 你好,我刚试过。它按预期工作。但是,我不知道为什么我所在的 ppl 试图不在另一个 viewModel 中有一个 viewModel 实例
  • @SebastianSchulz 你好,你能详细说明一下吗?它是使用动作/事件之类的吗?对不起。 Reli 是这个平台的新手哈哈..
  • 如果他们有理由不直接引用它们,您可以为此使用调解器。它只是一个类(通常是单例),不同的 ViewModel 向其注册、注销和发送消息。 (类似于 EventHandling 的一种更间接的方式)

标签: c# wpf xaml mvvm binding


【解决方案1】:

我认为你应该使用 Messenger。
这是一篇关于 mvvm 架构中 Messenger 的好文章。
有点旧,但它可以帮助你imo:https://msdn.microsoft.com/en-us/magazine/jj694937.aspx

这里是一个例子,给你。例如,我使用 Galasoft 的 MVVM Light,尽可能地简单:) http://www.galasoft.ch/mvvm/

首先,我建议您创建一个与您要广播的消息相关的实体:
我添加了一个简单的字符串属性,但显然你可以添加任何你想要的:)

public class Vm1toVm2Message
{
    public String Message { get; set; }
}

然后在您的 VM1 中创建您的消息并广播它:

public class ViewModel1 : ViewModelBase
{
    private RelayCommand _refreshCommand;
    public RelayCommand RefreshCommand
    {
        get
        {
            return _refreshCommand ?? (_refreshCommand = new RelayCommand(() =>
            {
                // You button command code
                // -------------------

                // Send a message
                Messenger.Default.Send<Vm1toVm2Message>( new Vm1toVm2Message { Message = "Update from VM1" });
            }));
        }
    }
}

最后在您的 VM2 中,您等待收到的消息 :)

public class ViewModel2 : ViewModelBase
{
    public ViewModel2()
    {
        Messenger.Default.Register<Vm1toVm2Message>(this, HandleVm1toVm2Message);
    }

    private void HandleVm1toVm2Message(Vm1toVm2Message msg)
    {
        // Do what you want here
    }
}

【讨论】:

  • 我同意这一点,但它应该是评论,而不是答案。除非您可以详细说明并提供示例。
【解决方案2】:

你可以试试这个:

public class FirstViewModel : INotifyPropertyChanged
{
    public delegate void PropertyChangedHandler(object obj);
    public static event PropertyChangedHandler MyPropertyChanged = delegate { };
    public FirstViewModel()
    {
        //Example: here I fire the function in the second ViewModel with parameter
        var obj = new { Name = "Jhon" };
        MyPropertyChanged(obj);
    }
}

第二个视图模型

public class SecondViewModel : INotifyPropertyChanged
{
    public SecondViewModel()
    {
        FirstViewModel.MyPropertyChanged += OnMyPropertyChanged;
    }

    public void OnMyPropertyChanged(object obj)
    {
        //...
    }

    //....
}

【讨论】:

    【解决方案3】:

    在视图模型 1 中,您可以存储更新视图模型 2 中属性的操作。在视图模型 1 中,您不能调用该操作并且视图模型 2 已更改。

    public class SomeOtherClass
    {
        public void main()
        {
            var vm1 = new ViewModel1();
            var vm2 = new ViewModel2();
    
            vm1.ChangeValueAction = new Action(() => { vm2.SomeProperty = String.Empty; });
        }
    }
    
    public class ViewModel1
    {
        public Action ChangeValueAction { get; set; }
    
        public void SomeMethod()
        {
            ChangeValueAction.Invoke();
        }
    }
    
    public class ViewModel2
    {
        public string SomeProperty { get; set; }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-02-23
      • 2018-09-20
      • 1970-01-01
      • 1970-01-01
      • 2011-01-23
      • 2011-06-21
      • 1970-01-01
      • 2021-04-22
      相关资源
      最近更新 更多