Problem of bindig in WPF with MVVM

1

I am working on a small solution that contains two projects. One is a data source and the other a WPF with MVVM. The code is uploaded in link

The fact is that when I open a new window from the MainWindow by means of a button with its corresponding command, The ViewModel of the new window does not correctly match the view. the DataContext of the window brings me correctly the target of the data source.

The view has this code:

<StackPanel Grid.Column="0" Grid.Row="1" 
     HorizontalAlignment="Center" Margin="26,30" Width="68">
        <Label Content="Nombre      :   " Margin="0,0,0,10"></Label>
        <Label Content="Apellidos   :   " Margin="0,0,0,10"></Label>
        <Label Content="Direccion   :   " Margin="0,0,0,10"></Label>
        <Label Content="Contacto    :   " Margin="0,0,0,10"></Label>
</StackPanel>
<StackPanel Grid.Column="1" Grid.Row="1" 
       HorizontalAlignment="Center"   Margin="40,30" Width="220" 
       DataContext = "{Binding}" >
    <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="{BindingPath=Apellidos,
           Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"> 
    </TextBox>
    <TextBox x:Name="txtDireccion" Width="160" Margin="0,0,0,18"
         Text="{ Binding Path=TempPersona.Direccion,    Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}">
    </TextBox>
    <TextBox x:Name="txtContacto" Width="160" Margin="0,0,0,18" 
        Text="{ Binding Path=TempPersona.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="Limpiar" Height="30" Width="120"  Margin="0,0,20,10"                
                CommandParameter="{Binding}"
                Command="{Binding 
           RelativeSource={RelativeSource 
          AncestorType={x:Type  Window}}, Path=DataContext.ClearAVM}">
    </Button>
        <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 The Codebehind:

public partial class AnadirView : Window
{
    public AnadirView(AnadirViewModel avm)
    {
        //AnadirViewModel avm = new AnadirViewModel(target);

        this.DataContext = avm;
        InitializeComponent();
    }
 }

And the ViewModewl:

public class AnadirViewModel : NotifyBase
{
    public IFactory targetAVM { get; set; }

    private Persona _tempPersona;
    public Persona TempPersona
    {
        get { return _tempPersona; }
        set
        {
            if (_tempPersona != value) //(1)
            {
                _tempPersona = value; 
                OnPropertyChanged("TempPersona");
            }
         }
    }
   // public AnadirViewModel() { }
    public AnadirViewModel(IFactory factory)
    {
        targetAVM = factory;

        AnadirView anadir = new AnadirView(this);
        anadir.Width = 400;
        anadir.Height = 400;
        anadir.ShowDialog();

    }
 ...
private ICommand _anadirAVM;
    public ICommand AnadirAVM
    {
    get { return _anadirAVM ??  //(2)
            (_anadirAVM = new RelayCommand((parameter) =>  AnadirAVMAction(parameter))); }
    }

    private void AnadirAVMAction(object parameter)
    {        
        TempPersona = parameter as Persona;
        targetAVM.Crear(TempPersona);
        targetAVM.GetAll();
        MessageBox.Show("Registro Añadir");
    }

This parameter always arrives null, because the TempPersona property is not updated. In (1) and (2) is the fault.  TempPersona is a property present in each target  The idea is to be able to change hot from a menu on the MainWindow and be able to insert, delete edit etc.  I have managed to change the target (MOCData and EFData) and delete it correctly, but at the time of adding it does not bin me the values of the Textbox with the TempPersona property. Is it a design problem?  Can someone think of something?

Greetings and thanks. Cesar

I discovered that the problem may come from the MainWindow.

<StackPanel Grid.Column="0" >
            <Button x:Name="Buscar" Height="40" Width="180"   Margin="20" Content="Buscar" 
                        Style="{StaticResource ButtonStyle}"
                        CommandParameter="{Binding ElementName=ListViewPersonas, Path=SelectedItems}"  
                        Command="{Binding Buscar}"/>
                <Button x:Name="btnAnadir"  Height="40" Width="180" Margin="20" Content="Añadir"  
                        Style="{StaticResource ButtonStyle}"
                        CommandParameter="{Binding}"
                        Command="{Binding Anadir}"/>
                <!--Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.Anadir}"/>-->
                <Button x:Name="btnNuevo"   Height="40" Width="180" Margin="20" Content="Editar" 
                        Style="{StaticResource ButtonStyle}"
                        CommandParameter="{Binding ElementName=ListViewPersonas, Path=SelectedItems}"
                        Command="{Binding Editar}"/>
                <!--Command="{Binding RelativeSource={RelativeSource AncestorType={x:Type Window}}, Path=DataContext.Editar}"/>-->
                <Button x:Name="btnBorrar"  Height="40" Width="180" Margin="20" Content="Borrar"  
                        Style="{StaticResource ButtonStyle}"
                        CommandParameter="{Binding ElementName=ListViewPersonas, Path=SelectedItems}"  
                        Command="{Binding Borrar}" />
                <Button x:Name="btnOpen"    Height="40" Width="180" Margin="20" Content="Visual"  
                       Style="{StaticResource BigFontButton}"
                        CommandParameter="" Command="{Binding Visual}"/>
            </StackPanel>
            <StackPanel Grid.Column="1">
                <ScrollViewer>
                    <ListView x:Name="ListViewPersonas" Grid.Row="1" Height="420" Width="520" 
                    HorizontalContentAlignment="Right"
                    ItemsSource="{Binding Path= DalPersona, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged }" >
                        <ListView.View>
                            <GridView x:Name="grdPersonas" >
                                <GridViewColumn Header="Nombre"    x:Name="Nombre"   DisplayMemberBinding="{Binding Nombre,     Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"   Width="160" />
                                <GridViewColumn Header="Apellidos" x:Name="Apellido" DisplayMemberBinding="{Binding Apellido,   Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"   Width="160" />
                                <GridViewColumn Header="Contacto"  x:Name="Contacto" DisplayMemberBinding="{Binding Contacto,   Mode=TwoWay,UpdateSourceTrigger=PropertyChanged}"   Width="220" />
                            </GridView>
                        </ListView.View>
                    </ListView>
                </ScrollViewer>
            </StackPanel>

All buttons have a CommandParameter linked to the item selected in the ListView, except that of Add. Any idea to fix it?

Thanks in advance and a sludo. César

    
asked by CesarIriso 22.09.2018 в 08:59
source

0 answers