2016-05-31 4 views
0

コレクションからグリッドコンテンツビューへのデータバインディングアイテムに問題があります。ラベルを付けるコレクションアイテムとxamarinフォームのテキストビュー

下記のコードをご覧ください。これは表現のためのものであり、完全なロジックの一部です。

ビューモデルクラス

public class ViewModel : INotifyPropertyChanged 
    { 
     private ObservableCollection<ChildViewModel> myProperty; 

     public ObservableCollection<ChildViewModel> MyProperty 
     { 
      get 
      { 
       return myProperty; 
      } 
      set 
      { 
       if (myProperty != value) 
       { 
        myProperty = value; 

        if (PropertyChanged != null) 
        { 
         PropertyChanged(this, 
          new PropertyChangedEventArgs("MyProperty")); 
        } 
       } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
    } 

    public class ChildViewModel : INotifyPropertyChanged 
    { 
     private string prop1; 
     private string prop2; 

     public string Prop1 
     { 
      get 
      { 
       return prop1; 
      } 
      set 
      { 
       if (prop1 != value) 
       { 
        prop1 = value; 

        if (PropertyChanged != null) 
        { 
         PropertyChanged(this, 
          new PropertyChangedEventArgs("Prop1")); 
        } 
       } 
      } 
     } 
     public string Prop2 
     { 
      get 
      { 
       return prop2; 
      } 
      set 
      { 
       if (prop2 != value) 
       { 
        prop2 = value; 

        if (PropertyChanged != null) 
        { 
         PropertyChanged(this, 
          new PropertyChangedEventArgs("Prop2")); 
        } 
       } 
      } 
     } 

     public event PropertyChangedEventHandler PropertyChanged; 
    } 

ここでビューモデルクラスの使用

ChildViewModel objChildViewModel1 = new ChildViewModel { Prop1 = "Prop1", Prop2 = "Prop2" }; 
     ChildViewModel objChildViewModel2 = new ChildViewModel { Prop1 = "Prop1", Prop2 = "Prop2" }; 

     ViewModel obj = new ViewModel 
     { 
      MyProperty = new ObservableCollection<ChildViewModel> { objChildViewModel1,objChildViewModel2} 
     }; 

     //set binding context 
     this.BindingContext = obj; 

     var grid = new Grid(); 
     grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); 
     grid.RowDefinitions.Add(new RowDefinition { Height = new GridLength(1, GridUnitType.Star) }); 
     grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); 
     grid.ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(1, GridUnitType.Star) }); 

     var Label1 = new Label { }; 
     //how to databind obj.MyProperty[0].Prop1 to Label1 ??? 

     var Label2 = new Label { }; 
     //how to databind obj.MyProperty[1].Prop1 to Label2 ??? 

     var Entry1 = new Entry { }; 
     //how to databind obj.MyProperty[0].Prop2 to Entry1 ??? 

     var Entry2 = new Entry { }; 
     //how to databind obj.MyProperty[1].Prop2 to Entry2 ??? 

     grid.Children.Add(Label1, 0, 0); 
     grid.Children.Add(Entry1, 0, 1); 
     grid.Children.Add(Label2, 1, 0); 
     grid.Children.Add(Entry2, 1, 1); 

私はコードに示されているように、私はLabel1のためのViewModelクラスのオブジェクトからコレクションをデータバインドしたいです、Label2、Entry1およびEntry2を含む。私は基本的なデータバインディングについて知っていますが、ここでは名前を持つプロパティをバインドするだけで、xamarinフォームで表示/制御できますが、ここではバインドできません。親切に私をここで助けてください。

答えて

0
grid.BindingContext=obj; 


Label1.SetBinding (Label.TextProperty, "MyProperty[0].Prop1"); 
Label2.SetBinding (Label.TextProperty, "MyProperty[1].Prop1"); 
Entry1.SetBinding (Entry.TextProperty, "MyProperty[0].Prop2"); 
Entry2.SetBinding (Entry.TextProperty, "MyProperty[1].Prop2"); 
+0

ありがとうございました。私はデータバインディングに関連するもう一つの問題がありますが、これはもっと難解です。 http://stackoverflow.com/questions/37554497/binding-collaction-of-items-into-listviews-custom-viewcell-dynamically-in-xamarをご覧ください。 –

関連する問題