2017-05-31 132 views
0

我想我进入我的标签类绑定我的视图模型里面,所以每当我进入的变化,我的标签,我认为模型内部类也改变Xamarin表单绑定嵌套

这里是我的代码

型号

public class MyModel 
    { 
     public string Name { get; set; } 
     public string Description { get; set; } 
    } 

视图模型

public class MyViewModel : INotifyPropertyChanged 
{ 
    public MyViewModel() 
    { 
     Model = new MyModel(); 
    } 

    private MyModel _Model; 
    public MyModel Model 
    { 
     get { return _Model; } 
     set 
     { 
      _Model = Model; 
      OnPropertyChanged(); 
     } 
    } 

    public event PropertyChangedEventHandler PropertyChanged; 

    public void OnPropertyChanged([CallerMemberName]string propertyName = null) 
    { 
     PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
    } 
} 

BehindCode

public partial class Page1 : ContentPage 
    { 
     public Page1() 
     { 
      InitializeComponent(); 
      BindingContext = new MyViewModel(); 
     } 
    } 

PAGE

<?xml version="1.0" encoding="utf-8" ?> 
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
      xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
      x:Class="RKE.Page1"> 
    <StackLayout> 
     <Label Text="{Binding Model.Name}"/> 
     <Entry Text="{Binding Model.Name}"/> 
    </StackLayout> 
</ContentPage> 
+0

这是什么问题? – Krishna

+0

我想制作标签,并且每当条目更改时我的视图模型都会更改。你可以帮我吗? –

回答

1

你需要实现的inotify的模式也

public class MyModel:INotifyPropertyChanged 
    { 
     private string _Name; 
     public string Name { get { return _Name; } set { _Name = value; 
OnPropertyChanged(); } } 
     private string _Description; 
     public string Description { get{ return _Description; }set{ _Description = value; OnPropertyChanged(); } } 
    } 

public event PropertyChangedEventHandler PropertyChanged; 

public void OnPropertyChanged([CallerMemberName]string propertyName = null) 
{ 
    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); 
} 
+0

酷!有用。谢谢。 –

+0

不客气 – Krishna