Hi, all.
I have a StackPanel, with several TextBox and I want to link the Text property to a property of type Person declared in the ViewModel of the view. The address should go from the view to the ViewModel, but I can not do it.
What I have tried:
This is the code in View:
<StackPanel Grid.Column="1" Grid.Row="1"
HorizontalAlignment="Center" Margin="40,30" Width="220"
DataContext="{Binding TempPersona,Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}">
<TextBox x:Name="txtNombre" Width="160" Margin="0,6,0,18"
Text="{ Binding Path=Nombre,
Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
</TextBox>
<TextBox x:Name="txtApellidos" Width="160" Margin="0,0,0,18"
Text="{ Binding Path=Apellidos, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}"></TextBox>
<TextBox x:Name="txtDireccion" Width="160" Margin="0,0,0,18"
Text="{ Binding Path=Direccion, Mode=TwoWay,
UpdateSourceTrigger=PropertyChanged}">
</TextBox>
<TextBox x:Name="txtContacto" Width="160" Margin="0,0,0,18"
Text="{ Binding Path=Contacto,
Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
</TextBox>
</StackPanel>
<StackPanel Orientation ="Horizontal"
Grid.ColumnSpan="2" Grid.Row="2"
HorizontalAlignment="Center" Margin="70,0" Width="280">
<Button Content="Añadir" Height="30" Width="120" Margin="20,0,0,10"
CommandParameter ="{Binding}"
Command="{Binding RelativeSource={RelativeSource
AncestorType={x:Type Window}},
Path=DataContext.AnadirAVM}">
</Button>
</StackPanel>
In ViewModel :
private Persona _tempPersona;
public Persona TempPersona
{
get { return _tempPersona; }
set
{
if (_tempPersona != value)
{
_tempPersona = value;
OnPropertyChanged("TempPersona");
}
}
}
private ICommand _anadirAVM;
public ICommand AnadirAVM
{
get { return _anadirAVM ??
(_anadirAVM = new RelayCommand((parameter)
=> AnadirAVMAction(parameter))); }
}
private void AnadirAVMAction(object parameter)
{
TempPersona = parameter as Persona;
targetAVM.Crear(TempPersona);
MessageBox.Show("Register Added");
}
Here parameter always comes to null.
Thank you in advance. Cesar