设置ListView itemsource在Xamarin表单后面的代码中绑定



我似乎无法直接解决此问题。我试图在后面的代码中设置项目源。这在XAML中很容易做到,但在后面的代码中似乎并不那么直接。

在背后的代码中,我正在使用:

Binding listbind = new Binding("routeLabels") {Source=this};
listviewofroutes.ItemsSource = SetBinding(ListView.ItemsSourceProperty, listbind);

这只是丢下" CAND将void转换为system.collections.ienumerable"的错误,我也认为这不是正确的。

im试图将其绑定到视图模型中可观察的集合。

视图模型:

private ObservableCollection<RouteInfo> _routelabels;
    public ObservableCollection<RouteInfo> routeLabels
    {
        get { return _routelabels; }
        set
        {
            if (Equals(value, _routelabels)) return;
            _routelabels = value;
            OnPropertyChanged(nameof(routeLabels));
        }
    }

在XAML中设置绑定时,此绑定正常。问题不是可观察到的收集,问题是我不知道如何在后面的代码中设置绑定。

摘要:

我需要知道如何执行此操作(项目库绑定):

<ListView x:Name="listviewofroutes" ItemsSource="{Binding routeLabels}">

</ListView>

在后面的代码中。

任何帮助将不胜感激。

以在控件上编程设置绑定,您必须将其作为扩展方法中的参数传递。参考链接:扩展方法或成员方法

例如,尝试:

listviewofroutes.SetBinding(ListView.ItemsSourceProperty, "routeLabels")
//Or, 
listviewofroutes.SetBinding(ListView.ItemsSourceProperty, new Binding("routeLabels")) 

这将路径为"路由"和控制的BindingContext之间的绑定。

此外,根据C#属性的标准命名策略,建议将"路由"更改为"路由"。

最新更新