Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/design-patterns/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 将数据从视图传递到viewmodel_Wpf_Mvvm - Fatal编程技术网

Wpf 将数据从视图传递到viewmodel

Wpf 将数据从视图传递到viewmodel,wpf,mvvm,Wpf,Mvvm,我想将数据从视图构造函数传递到视图模型 这是我的视图,它有一个IEnumerable类型参数 public SearchView(IEnumerable<ISearchable> enumerable) { InitializeComponent(); } 我如何才能做到这一点。非常感谢您的帮助。请确保在加载视图时可以做到这一点。看看我的伪代码: dependency property of type object objPropert

我想将数据从视图构造函数传递到视图模型 这是我的视图,它有一个IEnumerable类型参数

    public SearchView(IEnumerable<ISearchable> enumerable)
    {
        InitializeComponent();
    }

我如何才能做到这一点。非常感谢您的帮助。

请确保在加载视图时可以做到这一点。看看我的伪代码:

   dependency property of type object objProperty = null;

   private object tempObj;

   public MyWindow(object param) : base()
   {
      tempObj = param;

      // create one way to source Binding
      Binding b = new Binding("DataContext.ViewModelProperty");
      b.Source = this;
      b.Mode = BindingMode.OneWayToSource;
      this.SetBinding(b, objProperty);

      // listen to DataContext changes
      this.DataContextChanged += OnDataContextChanged
   }

   private void OnDataContextChanged(...)
   {
       // push data to ViewModel 
       this.SetCurrentValue(objProperty, tempObj);
   }
这就是允许数据从视图到ViewModel的方式


每次DataContext或更好的说法是ViewModel发生更改时,您都会通过绑定到源来推送值。

在我的例子中,我想访问IEnumerable enumerable,它是ViewModel中的构造函数参数。我该怎么做呢?请你详细说明一下。谢谢你好像在找Unity Container看看这里
   dependency property of type object objProperty = null;

   private object tempObj;

   public MyWindow(object param) : base()
   {
      tempObj = param;

      // create one way to source Binding
      Binding b = new Binding("DataContext.ViewModelProperty");
      b.Source = this;
      b.Mode = BindingMode.OneWayToSource;
      this.SetBinding(b, objProperty);

      // listen to DataContext changes
      this.DataContextChanged += OnDataContextChanged
   }

   private void OnDataContextChanged(...)
   {
       // push data to ViewModel 
       this.SetCurrentValue(objProperty, tempObj);
   }