2017-03-18 3 views
3

Ich habe eine Liste von Elementen mit einem Eintrag unter jedem Etikett. Ich möchte die Elemente der Liste "Element" in einer Zeile mit einem Eintrag, jetzt sind sie untereinander. Ich möchte das Label auf der linken Seite und den Eintrag auf der rechten Seite in einer Zeile.Xamarin Forms Etikett und Eintrag in einer Zeile

mein Code so weit

StackLayout layout = new StackLayout(); 

     for(int i = 0; i < item.Count; i++) 
     { 
      var label = new Label 
      { 
       Text = item[i] 
      }; 
      var entry = new Entry(); 
      Grid stackLayout = new Grid 
      { 
       HorizontalOptions = LayoutOptions.CenterAndExpand, 
       Children = { 
        label, 
        entry 
       }, 

      }; 

      layout.Children.Add(stackLayout); 
     } 

Antwort

0
StackLayout layout = new StackLayout(); 

for(int i = 0; i < item.Count; i++) 
{ 
    StackLayout innerLayout = new StackLayout 
    { 
     Orientation = StackOrientation.Horizontal 
    } 
    var label = new Label 
    { 
     Text = item[i] 
    }; 
    var entry = new Entry(); 
    innerLayout.Children.Add(label); 
    innerLayout.Children.Add(entry); 
    layout.Children.Add(innerLayout); 
} 

ich nicht, dass nicht getestet, aber hoffentlich wird es Ihnen helfen :)

3

StackLayout mit Orientierung = ist horizontal die richtige Wahl. Ich empfehle auch, eine ListView zu verwenden, wenn Sie eine Liste von Items haben. Erstellen Sie eine DataTemplate mit einer ViewCell mit einem horizontalen Stacklayout

1

Ich erstellte ein benutzerdefiniertes Steuerelement für Sie zu implementieren. Mit diesem Steuerelement können Sie das Steuerelement verwenden, ohne die Ansicht in irgendeiner Weise anpassen zu müssen.

Custom Control

using Xamarin.Forms; 

namespace StackOverflowHelp 
{ 
    /// <summary> 
    /// Custom <see cref="Grid"/> containing <see cref="Label"/> next to <see cref="Entry"/> 
    /// </summary> 
    public class EntryTextOneLine : Grid 
    { 
     /// <summary> 
     /// <see cref="Style"/> for <seealso cref="_text"/> 
     /// </summary> 
     private static Style _textStyle = new Style(typeof(Label)) 
     { 
      Setters = 
      { 
       new Setter { Property = Label.TextColorProperty, Value = Color.Black }, 
       new Setter { Property = Label.FontAttributesProperty, Value = FontAttributes.Bold }, 
       new Setter { Property = HorizontalOptionsProperty, Value = LayoutOptions.FillAndExpand }, 
       new Setter { Property = VerticalOptionsProperty, Value = LayoutOptions.CenterAndExpand }, 
       new Setter { Property = Label.HorizontalTextAlignmentProperty, Value = TextAlignment.End } 
      } 
     }; 
     /// <summary> 
     /// label next to entry 
     /// </summary> 
     private Label _text = new Label 
     { 
      Style = _textStyle 
     }; 
     /// <summary> 
     /// <see cref="Entry"/> 
     /// </summary> 
     private Entry _entry = new Entry 
     { 
      VerticalOptions = LayoutOptions.CenterAndExpand, 
      HorizontalOptions = LayoutOptions.FillAndExpand 
     }; 

     /// <summary> 
     /// <see cref="Label.Text"/> next to <see cref="_entry"/> 
     /// </summary> 
     public string EntryText { 
      get { 
       return _text.Text; 
      } 
      set { 
       _text.Text = value; 
      } 
     } 
     /// <summary> 
     /// Custom <see cref="Grid"/> containing <see cref="Label"/> next to <see cref="Entry"/> 
     /// </summary> 
     public EntryTextOneLine() 
     { 
      ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.3, GridUnitType.Star) }); 
      ColumnDefinitions.Add(new ColumnDefinition { Width = new GridLength(0.7, GridUnitType.Star) }); 

      RowDefinitions.Add(new RowDefinition { Height = GridLength.Star }); 

      Children.Add(_text, 0, 0); 
      Children.Add(_entry, 1, 0); 
     } 
    } 
} 

Wie Steuerung implementieren
einfach ein neues EntryTExtOneLine-Objekt erstellen und es dem StackLayout hinzuzufügen.

using Xamarin.Forms; 

namespace StackOverflowHelp 
{ 
    public class EntryStackOverflow : ContentPage 
    { 
     private StackLayout _stack = new StackLayout(); 


     public EntryStackOverflow() 
     { 

      var list = new[] 
      { 
       new { title = "testing title" }, 
       new {title = "another title"}, 
       new {title = "text wraps down to the next line if too long"}, 
       new {title = "you get the point"} 
      }; 



      foreach(var n in list) 
      { 
       var control = new EntryTextOneLine 
       { 
        EntryText = n.title 
       }; 

       _stack.Children.Add(control); 
      } 

      Content = _stack; 
     } 
    } 
} 

Ergebnis

enter image description here

Verwandte Themen