1

I'm using mvvm light with wpf.

Currently i can pass string parameter to viewmodel's command like below:

<TextBox Height="23" TextWrapping="Wrap" Text="TextBox" Name="textbox1"/>
<Button Command="{Binding ShowMessage}" Content="Click Me"
        CommandParameter="{Binding ElementName=textbox1, Path=Text}" />

My question is how to pass composite type like Person to a command of ViewModel?

Thanks

Rohit Vats
  • 79,502
  • 12
  • 161
  • 185
r.zarei
  • 1,261
  • 15
  • 35

1 Answers1

1
<TextBox Height="23" TextWrapping="Wrap" Text="TextBox" Name="textbox1" Tag="{Binding Person}"/>
<Button Command="{Binding ShowMessage}" Content="Click Me"
CommandParameter="{Binding ElementName=textbox1, Path=Tag}" />

you can make use of Tag Property of TextBox but I think there is something wrong with your MVVM implementation,It is the ViewModel that holds the data for your view. But here you sending it from View to VM . I mean Person should be automatically there in your VM instead of sending it from View.

yo chauhan
  • 12,079
  • 4
  • 39
  • 58
  • @you misunderstood me, suppose i have a windows (view) for creating person (inserting), how should i pass person data to viewmodel? – r.zarei Jul 21 '13 at 17:26
  • 2
    r.zarei, you misunderstood @ethicallogics. :) Your VM should create the Person object in response to a Command sent from the Button. In other words, all your logic is in the VM - the View is just a trigger. – New Dev Jul 21 '13 at 19:07