2017-01-23 53 views
0

我有一個自定義控件,基本上是一個stacklayout,我可以綁定一個串聯的字符串,並將其分解爲多個對象,如標籤列表。xamarin表單自定義控件

public void Render() 
     { 
      if (ItemsSource == null) 
       return; 

      this.Orientation = Orientation == StackOrientation.Vertical ? StackOrientation.Vertical : StackOrientation.Horizontal; 
      this.Margin = new Thickness (0, 0, 0, 0); 

      List<string> items = ItemsSource.Split('|').ToList(); 
      foreach (var item in items) 
      { 
       var frame = new Frame(); 
       frame.BindingContext = item; 
       frame.BackgroundColor = Color.FromHex("#FCFACF"); 
       frame.OutlineColor = Color.FromHex("#D0C0AD"); 
       frame.HasShadow = false; 
       frame.Padding = new Thickness(4, 2, 4, 0); 
       var label = new Label(); 
       label.Text = "{Binding}"; 
       label.FontSize = 10; 
       label.Parent = frame; 
       Children.Add(frame); 
      } 
     } 

,但我似乎無法得到這個權利,我如何添加一個標籤,可以是一個框架的孩子,我怎麼能解決我的結合,截至目前,如果我的標籤添加到堆疊佈局的子標籤的文本是{binding},而不是實際的文本。

有人可以幫助我這個。如果我在XAML中添加了一個帶有datatemplate和viewcell的ItemTemplate,但我不想在XAML中完成此操作,因爲我希望在其他視圖中重用所有這些。

回答

0

幀Content屬性

Frame.Content = label; 

要分配代碼的綁定,使用

label.SetBinding(Label.TextProperty, new Binding(".")); 
+0

完美的感謝! –