Silverlight将值从视图传递到ViewModel MVVM

时间:2012-02-26 00:11:45

标签: silverlight mvvm mvvm-light

您好我有一个文本框,其文本最初通过数据绑定填充到一个值。

    <TextBox Name="EmployeeName" Text="{Binding Employee.Name}" Grid.Row="0" Grid.Column="1"  VerticalAlignment="Center" Margin="8,0,0,0"/>

我有一个刷新按钮,可以将其重置为原始值,还有一个保存按钮,可以保存更改。

    <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}"/>

我正在使用MVVM light并且在我的View模型中我创建了2个RelayCommands

    SaveEmployeeNameCommand = new RelayCommand(SaveEmployee);

    RefreshEmployeeNameCommand = new RelayCommand(RefreshEmployee);

    private void SaveEmployee()
    {

//如何从文本框(命令参数)获取值          }

1 个答案:

答案 0 :(得分:1)

我认为您不需要将名称作为CommandParameter传递。只要员工的Name属性具有公共setter并且您在文本框中使用双向绑定,就可以使用this.Employee.Name获取员工的姓名。

相关问题