wpf 如何将视图构造函数参数传递给视图模型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29535148/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:04:02  来源:igfitidea点击:

How to Pass View Constructor Parameter to View Model

c#wpfmvvm

提问by Simsons

I have a View and ViewModel.

我有一个视图和视图模型。

MyView
{
  MyView(int id)
  {
    InitializeComponent();
  }
}

 MyViewModel
    {
     private int _id;
      public int ID
       {
        get { return _id; }
        set { _id= value; OnPropertyChanged("_id"); }
       }
    }

I am setting the data Context on my XAML as :

我将 XAML 上的数据上下文设置为:

<Window.DataContext>
        <vm:MyViewModel/>
</Window.DataContext>

On Click of a button I am showing my View as :

单击按钮时,我将视图显示为:

Application.Current.Dispatcher.Invoke(() => new MyView(id).Show());

Now , How do I pass id from MyView(id)to MyViewModel.ID.

现在,我如何将 id 传递MyView(id)MyViewModel.ID.

采纳答案by Lennart

Since the DataContext gets created after the InitializeComponent() call, you can just pass it to the ViewModel afterwards:

由于 DataContext 是在 InitializeComponent() 调用之后创建的,因此您可以在之后将其传递给 ViewModel:

MyView
{
   MyView(int id)
   {
     InitializeComponent();
     ((MyViewModel)DataContext).ID = id;
   }
 }

回答by AymenDaoudi

You can do one of the following solutions :

您可以执行以下解决方案之一:

1) Use the Messengerpattern :

1)使用Messenger模式:

You can use the Messenger Pattern, if you are using MVVMLightfor example, you can do the following :

您可以使用 Messenger 模式,MVVMLight例如,如果您正在使用,则可以执行以下操作:

In the ViewModeldo :

ViewModel做:

MyViewModel
{
     private int _id;
     public int ID
     {
        get { return _id; }
        set { _id= value; OnPropertyChanged("_id"); }
     }

     Public void InitilizeMessenger()
     {
         Messenger.Default.Register(this,"To MyViewModel", (int id) => ID = id);
     }
     public MyViewModel()
     {
        InitilizeMessenger();
     }
}

You make the ViewModel ready for receiving messages by registering to the Messenger.

您可以通过注册到 Messenger 使 ViewModel 准备好接收消息。

in the View do :

在视图中执行:

MyView
{
  MyView(int id)
  {
    InitializeComponent();
    Messenger.Default.Send<int>(id,"To MyViewModel");
  }
}

Broadcast a message, by sending it accompanied by the "To MyViewModel" tag, so it can be caught by the MyViewModel.

广播一条消息,通过发送它伴随着“To MyViewModel”标签,所以它可以被 MyViewModel 捕获。

2) Access DataContext from the View :

2)从视图访问数据上下文:

MyView
{
  MyView(int id)
  {
    InitializeComponent();
    ((MyViewModel)this.DataContext).ID = id;
  }
}

The second solution is straightforward and simpler, I gave the first one just for in case of more complicated scenarios.

第二种解决方案简单明了,我给出了第一种解决方案,以防万一更复杂的情况。

回答by Doctor

Firstly, OnPropertyChanged("_id") is wrong, beacuse _id is a variable, not a property. You must change as OnPropertyChanged("ID"). You can assign viewModel in code behind.

首先, OnPropertyChanged("_id") 是错误的,因为 _id 是一个变量,而不是一个属性。您必须更改为 OnPropertyChanged("ID")。您可以在后面的代码中分配 viewModel。

class MyView
{
      MyView(int id)
        :this(new MyViewModel(id))
      {
      }

      MyView(MyViewModel viewModel)
      {
        InitializeComponent();
        this.DataContext = viewModel;
      }
}

class MyViewModel
{
     private int _id;
     public int ID
     {
        get { return _id; }
        set { _id= value; OnPropertyChanged("ID"); }
     }
     public MyViewModel(int id)
     {
        ID=id;
     }
}