Hi I have a textbox with a text that is initially populated to a value through Data Binding.
<TextBox Name="EmployeeName" Text="{Binding Employee.Name}" Grid.Row="0" Grid.Column="1" VerticalAlignment="Center" Margin="8,0,0,0"/>
And I have a refresh button that should reset it back to its original value and a Save button that should save the changes.
<Button Name="RefreshEmployeeName" Content="Refresh" Grid.Column="2" Grid.Row="0" Width="50" Height="25" Command="{Binding RefreshEmployeeNameCommand}" CommandParameter="{Binding Text, ElementName=EmployeeName}"/>
<Button Name="SaveEmployeeName" Content="Refresh" Grid.Column="2" Grid.Row="0" Width="50" Height="25" Command="{Binding SaveEmployeeNameCommand}" CommandParameter="{Binding Text, ElementName=EmployeeName}"/>
I am using MVVM lightand in my View Model I have created 2 RelayCommands
SaveEmployeeNameCommand = new RelayCommand(SaveEmployee);
RefreshEmployeeNameCommand = new RelayCommand(RefreshEmployee);
private void SaveEmployee()
{
// how to get the value from the text box (Command Parameter) here
}
I don’t think you need to pass the name as a CommandParameter. As long as the employee’s Name property has a public setter and you use a two-way binding on the text box, you can get the employee’s name using this.Employee.Name.