Reputation: 514
I have been searching the web, but can't find the answer i need.
I have a list where the user can choose some items. In my ViewModel it looks like this:
public ObservableCollection<RentalItemVM> ChosenRentalItems {
get { return chosenRentalItems; }
set {
chosenRentalItems = value;
RaisePropertyChanged("ChosenRentalItems");
}
}
When the user is done selecting, he presses a button to save his collection. It works fine if I setup my RelayCommand like this:
public RelayCommand<ObservableCollection<RentalItemVM>> FinishCommand { get; set; }
private void Finish(ObservableCollection<RentalItemVM> chosenItems) {
...
}
Now I need to get the UserId as well, so after some research I found out that I can't pass more than one paramater to the RelayCommand so I need a wrapper. I've made a new ViewModel:
public class OrderVM {
public int UserId { get; set; }
ObservableCollection<RentalItemVM> ChosenItems { get; set; }
}
and changed the RelayCommand to this:
public RelayCommand<OrderVM> FinishCommand { get; set; }
private void Finish(OrderVM order) {
...
}
And i changed the XAML UserControl from this:
<StackPanel Orientation="Horizontal"
Grid.Row="3"
Grid.ColumnSpan="2"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBox Text="UserId" />
<Button Content="Finish"
Command="{Binding FinishCommand}"
CommandParameter="{Binding ChosenRentalItems}"
UseLayoutRounding="False"
FontSize="24" />
</StackPanel>
to this:
<StackPanel Orientation="Horizontal"
Grid.Row="3"
Grid.ColumnSpan="2"
VerticalAlignment="Center"
HorizontalAlignment="Center">
<TextBox Text="UserId"
x:Name="UserId" />
<Button Content="Finish"
Command="{Binding FinishCommand}"
CommandParameter="{Binding OrderVM}"
UseLayoutRounding="False"
FontSize="24" />
</StackPanel>
Now this is where I get confused. I have no idea how to bind the UserId and ChosenItems. The OrderVM parameter in the RelayCommand is null, so maybe I need to instantiate it first?
Most answers that I found on the web were about displaying data from a ViewModel in a View, but almost no answers on how to post data (or I searched for the wrong things?)
Upvotes: 0
Views: 53
Reputation: 398
If the command FinishCommand
is in the same view model of the DataConext, you can bind UserId
too.
<TextBox Text="{Binding UserId} " x:Name="UserId"></TextBox>
and take UserId from your ViewModel instance where command is implemented.
private void Finish(ObservableCollection<RentalItemVM> chosenItems) {
var userid = this.UserId;
foo(userid);
}
Upvotes: 1