Click here to Skip to main content
15,879,326 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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) //Here value is null
                {
                    _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
Posted
Updated 15-Sep-18 1:54am

1 solution

It's hard to tell what the problem is from the code that you have posted. We can't see if you have correctly bound the VM to the View's DataContext, nor do you mention if you have checked the debug output for broken bindings.

So instead, here is a working answer similar to the problem that you have above: https://www.codeproject.com/Answers/1258762/Want-to-create-WPF-solution-in-MVVM-pattern#answer2
 
Share this answer
 
Comments
cesar.iriso 15-Sep-18 8:47am    
Thank you for the answer. This is a small project that I am preparing to try learn. I have uploaded it to OneDrive in this link :
https://1drv.ms/f/s!AlLQbFaWZ-ZTfjgp84azdbWq2J0
Basically has two projects, one with a DAL with several targets and the other has a menu that allows you to change DAL hot.
The specific view is a dialog to add elements.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900