Hi friends, I'm new here and I need a little help. I'm trying to use ListView to mount dynamic controls, that is, the controls appear only when I add an item in the list, however, this is not what is happening. If you add an item to the list when the page loads, the fields are shown, but if, at run time, adding another item to the list, the fields are not shown for this new item. The code is below. Please, can you tell me what I'm doing wrong? If it is not to use ListView in this case, which control is the best?
XAML:
<ContentPage.Content>
<StackLayout>
<StackLayout Orientation="Horizontal">
<Button x:Name="btnCancel" Image="btnCircleLeft_30px" Margin="0, 6, 0, 0" BackgroundColor="Transparent" HorizontalOptions="StartAndExpand"/>
<Button x:Name="btnNext" Image="btnCircleRight_30px" Margin="0, 6, 0, 0" BackgroundColor="Transparent" HorizontalOptions="EndAndExpand" Command="{Binding CommandButtonNext}" />
</StackLayout>
<ScrollView Orientation="Vertical" VerticalOptions="StartAndExpand">
<StackLayout Orientation="Horizontal">
<Label Text="Telefones" TextColor="White" FontSize="Medium" HorizontalOptions="StartAndExpand" Margin="12, 12, 0, 0"/>
<Button x:Name="btnAddPhone" Image="btnCircleAdd_15px" BackgroundColor="Transparent" HorizontalOptions="EndAndExpand" Margin="0, 12, 0, 0" Command="{Binding CommandAddPhone}"/>
</StackLayout>
<StackLayout>
<ListView ItemsSource="{Binding Path=PhoneObsList, Mode=TwoWay} ">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<StackLayout Orientation="Vertical">
<StackLayout x:Name="PhoneArea" Orientation="Horizontal">
<Entry x:Name="phoneNumber" Text="{Binding Path=Number}" Placeholder="(99)999.999.999" Margin="10, 0, 0, 0" TextColor="White" PlaceholderColor="LightGray" FontSize="Small" HorizontalOptions="StartAndExpand"/>
<Picker x:Name="phoneType" Title="Selecione" ItemsSource="{Binding PhoneTypeList}" ItemDisplayBinding="{Binding Description}" SelectedItem="{Binding Path=PhoneType}" TextColor="White" Margin="10, 0, 0, 0" HorizontalOptions="CenterAndExpand" />
<Button x:Name="btnRemovePhone" Image="btnCircleRemove_15px" BackgroundColor="Transparent" HorizontalOptions="EndAndExpand" Margin="0"/>
</StackLayout>
</StackLayout>
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
</ScrollView>
</StackLayout>
</ContentPage.Content>
VM:
public class SignUpVM : INotifyPropertyChanged
{
public ICommand CommandAddPhone { get; set; }
public event PropertyChangedEventHandler PropertyChanged;
protected virtual void OnPropertyChanged([CallerMemberName]string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
private ObservableCollection<PhoneModel> phoneObsList;
public ObservableCollection<PhoneModel> PhoneObsList
{
get { return phoneObsList; }
set
{
phoneObsList = value;
}
}
public SignUpVM()
{
CommandAddPhone = new Command(async () =>
{
await OnAddPhoneCommand();
});
}
private async Task OnAddPhoneCommand()
{
if (PhoneObsList == null)
PhoneObsList = new ObservableCollection<PhoneModel>();
PhoneObsList.Add(new PhoneModel());
await Task.FromResult<object>(null);
}
}