locked
MVVM - Pass parameter to viewmodel constructor and async loading RRS feed

  • Question

  • Hi All,

    I am new to MVVM pattern. I want to pass parameter to viewmodel constructor and load data using ria services based on the parameter. How do I achieve this?

    Thursday, May 5, 2011 2:02 AM

Answers

  • Hi,

    Yes, You can do that but you can not create an instance of ViewModel via XAML instead of using View's code behind files.

    public class MainViewModel

    //Constructor

    public MainViewModel("Type that you want to pass" Variable )

    {

    }

    //how to use

    MainViewNodel vm = new MainViewModel(Variable);

    Saturday, May 7, 2011 7:46 AM
  • You COULD declare a dependency property in your viewmodel and just bind the attribute in the StaticResource.  Then do what you want to do in the property setter.

    That way there would be no code behind ("not that there is anything wrong with that" Seinfeld quote)

    Saturday, May 7, 2011 7:50 AM

All replies

  • Interesting If the Viewmodel is Declared as a Static Resource how can i pass parameter to the viewmodel constructor

    Thursday, May 5, 2011 3:27 AM
  • Hi,

    Yes, You can do that but you can not create an instance of ViewModel via XAML instead of using View's code behind files.

    public class MainViewModel

    //Constructor

    public MainViewModel("Type that you want to pass" Variable )

    {

    }

    //how to use

    MainViewNodel vm = new MainViewModel(Variable);

    Saturday, May 7, 2011 7:46 AM
  • You COULD declare a dependency property in your viewmodel and just bind the attribute in the StaticResource.  Then do what you want to do in the property setter.

    That way there would be no code behind ("not that there is anything wrong with that" Seinfeld quote)

    Saturday, May 7, 2011 7:50 AM
  • As I know you can also pass parameter to Construtor and Property.

    Wednesday, May 11, 2011 8:56 AM
  • Hi,

    In Prism we do not directly pass to ViewModel but through Type and Service. In general MVVM you can do that but it is not suitable for big solution.

    Sunday, May 22, 2011 8:32 AM
  • Can you please give a super easy example on how to do this dependency property in the viewmodel and bind the attribute in the StaticResource.  It would be super helpful!!!

    I have multiple views with one viewmodel - the reason is that they all share similar data so a 1:1 didn't make sense for me.  I do not want to load all of my collections up at once, I only want to load the collection that pertains to the current view that I'm in (user type views - not all users will see other views or their collections).  I thought the best way would be to pass a param into the CTOR of the VM, but I'm having issues with binding.  the operations are being loaded, but when they get back to the view everything is null.  i declared everything in the XAML (things were working great when I had just one VM per View, but now that I try to combine and want to do ondemand style loading based on the view you're in - I'm having issues).  I am not using any frameworks either, and I'd like to try to avoid doing the loading in the code behind if possible.

    I'm brand spanking new to SL4 Ria Services and come from a web forms background.  Any help is greatly appreciated, especially if it's really dumbed down.  There is just so much stuff out there for RIA services/SL newbies and it's hard to wade through it all.

    THANK YOU!!!

     

    Friday, August 5, 2011 11:46 AM
  • Property injection.

    use dependency attribute

    [Dependency]

    public string str {get; private set;}

    Sunday, August 7, 2011 1:16 PM
  • The link broken. Is this has the source for parameter passing in Prism
    Thursday, June 27, 2013 12:11 AM
  • Hi Dush_a,

    Take a look at following link at the Passing Parameters During Navigation section:

    http://msdn.microsoft.com/en-us/library/gg430861(v=pandp.40).aspx

    Sunday, September 28, 2014 6:50 AM
  • THIS ONE WORKS :)
    Saturday, February 18, 2017 8:06 PM