I am making a property app where I have one file for LandingPage.cs and another for LandingViewModel.cs. The goal is to bind a list of sections to radio buttons and set the first radio button as checked. However, I encounter an error when trying to set the first radio button as checked. Here is my code:
// LandingViewModel.cs
using PropertyApp.Model;
using PropertyApp.View;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;
namespace PropertyApp.ViewModel
{
public class LandingViewModel : BaseViewModel
{
public List<string> Sections = new List<string> { "Trending", "Popular", "Buy", "Rent" };
public List<Property> Properties => PropertyRepo.AllProperties;
public Property SelectedProperty { get; set; }
public ICommand PropertySelected => new Command(obj =>
{
if (SelectedProperty != null)
Application.Current.MainPage.Navigation.PushAsync(new DetailsPage(SelectedProperty));
SelectedProperty = null;
});
}
}
// LandingPage.cs
using PropertyApp.ViewModel;
namespace PropertyApp.View
{
public partial class LandingPage : ContentPage
{
public LandingPage()
{
InitializeComponent();
this.BindingContext = new LandingViewModel();
(SectionList.Children[0] as RadioButton).IsChecked = true;
}
}
}
using this line
(SectionList.Children[0] as RadioButton).IsChecked = true;
Got this error System.ArgumentOutOfRangeException: 'Index was out of range. Must be non-negative and less than the size of the collection. (Parameter 'index')
Can anyone tell me where I am going wrong? I want to activate the radio buttons for the list of sections given, but in the Android emulator, the buttons are not showing.
Unlike your Properties object, your SectionList is not updating the value sent to the UI
So this line
public List<string> Sections = new List<string> { "Trending", "Popular", "Buy", "Rent" };
should be
public List<string> Sections => new List<string> { "Trending", "Popular", "Buy", "Rent" };