CopyPastor

Detecting plagiarism made easy.

Score: 1; Reported for: Exact paragraph match Open both answers

Original Post

Original - Posted on 2023-08-30
by Alexandar May - MSFT



            
Present in both answers; Present only in the new answer; Present only in the old answer;

You need to wrap the `Label` with `VerticalStackLayout` and then add `HorizontalOptions="Center"` to it. Here's the code below for your reference:
``` <ScrollView> <VerticalStackLayout Margin="20" Spacing="25" Padding="30,0" VerticalOptions="Center">
<Entry x:Name="entry" MaxLength="255" MinimumWidthRequest="500" Placeholder="Name" FontSize="18" HorizontalOptions="Center" />
<Button x:Name="AddToList" Text="Add" Clicked="AddInputItemTolist" HorizontalOptions="Center" />
<ListView ItemsSource="{Binding MyNames}" BackgroundColor="Red" > <ListView.ItemTemplate> <DataTemplate> <ViewCell>
<VerticalStackLayout> <Label Text="{Binding Name}" HorizontalOptions="Center"></Label> </VerticalStackLayout> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>
</VerticalStackLayout>
</ScrollView>
```
If you want your Model: MyName being added to the `ListView`, the `ItemsSource` should be `ObservableCollection`.
Here's the code snippet below for your reference:
XAML: ``` <ScrollView> <VerticalStackLayout Margin="20" Spacing="25" Padding="30,0" VerticalOptions="Center">
<Entry x:Name="entry" MaxLength="255" MinimumWidthRequest="500" Placeholder="Name" FontSize="18" HorizontalOptions="Center" />
<Button x:Name="AddToList" Text="Add" Clicked="AddInputItemTolist" HorizontalOptions="Center" />
<ListView ItemsSource="{Binding MyNames}" > <ListView.ItemTemplate> <DataTemplate> <ViewCell> <Label Text="{Binding Name}"></Label> </ViewCell> </DataTemplate> </ListView.ItemTemplate> </ListView>
</VerticalStackLayout> </ScrollView> ```
Code-behind:
``` public partial class MainPage : ContentPage {
public ObservableCollection<MyName> MyNames { get;set;} = new ObservableCollection<MyName>();       public MainPage()       {             InitializeComponent();
BindingContext = this;       }
private void AddInputItemTolist(object sender, EventArgs e) { var myName = new MyName { Name = entry.Text }; MyNames.Add(myName); }
public class MyName { public string Name { get; set; }
} } ```

        
Present in both answers; Present only in the new answer; Present only in the old answer;