将对象xaml绑定到viewmodel中的属性 [英] Binding object xaml to property in viewmodel

查看:83
本文介绍了将对象xaml绑定到viewmodel中的属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好。

我有一个StackPanel,有几个TextBox,我想将Text属性链接到视图ViewModel中声明的Person类型的属性。地址应该从视图到ViewModel,但是我不能这样做。



我尝试了什么:



这是视图中的代码:

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>







在ViewModel中:






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");
}





这里参数总是为空。

谢谢你预先。 Cesar



Here parameter always comes to null.
Thank you in advance. Cesar

推荐答案

很难说出你发布的代码存在什么问题。我们无法看到您是否已将VM正确绑定到View的DataContext,也没有提及是否已检查调试输出是否存在损坏的绑定。



所以相反,这是一个类似于上面问题的工作答案:https://www.codeproject.com/Answers/1258762/Want-to-create-WPF-solution-in-MVVM-pattern#answer2
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


这篇关于将对象xaml绑定到viewmodel中的属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆