I hope you can help me. I have a view created in code that is bound to a viewModel and inside the view there is a ListView.
The problem is that my code that adds the controls, including the ListView, is in the view's constructor and has as a parameter the viewModel that I assign to the BindingContext of the view.
public SurveyQuestionsView(SurveyQuestionsViewModel vm)
{
_vm = vm;
BindingContext = _vm;
var listView = new ListView();
listView.ItemsSource = _vm.Questions;
Content = listView;
}
At the time the constructor is called on the viewModel my property that serves as the data source for the listView is still null. And it is through another procedure that this property of the viewModel is filled, but I can't find a way for the View to detect that change and because of that, reload the ListView.
public partial class SurveyQuestionsViewModel : ObservableObject
{
private readonly ISurveyApplication _surveyApplication;
public SurveyQuestionsViewModel(ISurveyApplication surveyApplication)
{
_surveyApplication = surveyApplication;
}
[ObservableProperty]
private ObservableCollection<SurveyQuestionModel> questions;
async Task LoadData()
{
var data = await _surveyApplication.GetSurveyQuestionsBySurveyId(currentSurvey.SurveyId);
Questions = new ObservableCollection<SurveyQuestionModel>(data);
}
}
Thank you very much for your help!!
You're not using data binding at all - you're just directly assigning ItemsSource
to setup data binding in code, use SetBinding
listView.SetBinding(ListView.ItemsSourceProperty, nameof(Questions));
since Questions
is Observable
you should not need to do anything special when you assign or re-assign the property's value