MVVMLight-将参数传递给ViewModel构造函数? [英] MVVMLight-- Passing a parameter to a ViewModel constructor?
本文介绍了MVVMLight-将参数传递给ViewModel构造函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设我有一个ListBox,该ListBox绑定到Foo
对象的集合,并且所选的Foo
显示在contentcontrol中,内容模板为FooView
.
Suppose I have a ListBox which is bound to a collection of Foo
objects, and the selected Foo
is displayed in a contentcontrol with the content template being a FooView
.
有没有一种方法可以使FooView
将选定的Foo
传递给FooViewModel
的构造函数,该构造函数是通过ViewModelLocator
的数据上下文?
Is there a way that I can make it so that the FooView
passes the selected Foo
to the constructor of the FooViewModel
which is it's datacontext via the ViewModelLocator
?
推荐答案
MainViewModel
MainViewModel
/*INotifyPropertChanged property*/
public FooViewModel CurrentFooVM{
get{/*INPC code*/}
private set{/*INPC code*/}
}
/*INotifyPropertChanged property*/
public Foo SelectedFoo{
get{/*INPC code*/}
set{/*INPC code*/ CurrentFooVM = new FooViewModel(_selectedFoo)}
}
public ObservableCollection<Foo> Foos {get; private set;}
MainView
<ListBox ItemsSource={Binding Foos}
SelectedItem={Binding SelectedFoo}>...
<FooView... bind to CurrentFooVM...
这篇关于MVVMLight-将参数传递给ViewModel构造函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文