MVVMLight - Passing a parameter to a ViewModel constructor?

Suppose I have a ListBox that is bound to a collection of Foo objects, and the selected Foo displayed in the content content with a content template equal to FooView .

Is there a way to make this so that FooView selected Foo the FooViewModel constructor, which is a datacontext via ViewModelLocator ?

+4
source share
2 answers

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... 
+3
source

I heard that although the toolkit in Visual Studio does not support it, the XML specification allows you to initialize a class with a parameter in the constructor. I have never done this, but I heard that this is possible on the .Net Rocks podcast. It may also be relevant only for WPF, not Silverlight, since WPF has more features than Silverlight. Not very helpful, but it can put you on the right track.

+1
source

Source: https://habr.com/ru/post/1332992/


All Articles