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