NET Maui以编程方式重新绑定ItemsSource

huangapple go评论63阅读模式
英文:

NET Maui Rebinding ItemsSource programmatically

问题

希望你能帮助我。我有一个在代码中创建的视图,它与一个viewModel绑定,视图内部包含一个ListView。

问题是,我的代码用于添加控件(包括ListView)的逻辑位于视图的构造函数中,并且构造函数的参数是我分配给视图的BindingContext的viewModel。

public SurveyQuestionsView(SurveyQuestionsViewModel vm)
{
    _vm = vm;
    BindingContext = _vm;

    var listView = new ListView();
    listView.ItemsSource = _vm.Questions;

    Content = listView;
}

在构造函数调用viewModel时,用作ListView数据源的属性仍然为null。要填充此viewModel属性,需要通过另一个过程,但我找不到一种方法来让视图检测到此更改,从而重新加载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);
    }
}

非常感谢您的帮助!

英文:

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&lt;SurveyQuestionModel&gt; questions;
	
	async Task LoadData()
    {
        var data = await _surveyApplication.GetSurveyQuestionsBySurveyId(currentSurvey.SurveyId);
       Questions = new ObservableCollection&lt;SurveyQuestionModel&gt;(data);
    }
}

Thank you very much for your help!!

答案1

得分: 0

你根本没有使用数据绑定 - 你只是直接分配 ItemsSource

要在代码中设置数据绑定,使用 SetBinding

listView.SetBinding(ListView.ItemsSourceProperty, nameof(Questions));

因为 QuestionsObservable,当你分配或重新分配属性的值时,你不应该需要做任何特殊的事情。

英文:

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

huangapple
  • 本文由 发表于 2023年6月19日 03:13:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/76502163.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定