MVVMLight— Passing a parameter to a ViewModel constructor?

泪湿孤枕 提交于 2019-12-05 12:55:38
Jake Berger

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...

I heard that although the tooling in Visual Studio does not support it, the XML spec does allow you to instanciate a class with a parameter in the constructor. I've never done it, but heard that this was possible on the .Net Rocks podcast. May also be only relevant to WPF as opposed to Silverlight, as WPF has more features than Silverlight. Not much help, but may put you on the right track.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!