use*_*961 2 c# xaml xamarin.forms
我似乎无法得到这个问题的直接答案。我试图在后面的代码中设置项目源。这在 Xaml 中很容易做到,但在后面的代码中似乎并不那么简单。
在我使用的后面的代码中:
Binding listbind = new Binding("routeLabels") {Source=this};
listviewofroutes.ItemsSource = SetBinding(ListView.ItemsSourceProperty, listbind);
Run Code Online (Sandbox Code Playgroud)
这只会引发“无法将 void 转换为 System.Collections.IEnumerable”的错误,我也认为这不正确。
我试图将它绑定到视图模型中的可观察集合。
视图模型:
private ObservableCollection<RouteInfo> _routelabels;
public ObservableCollection<RouteInfo> routeLabels
{
get { return _routelabels; }
set
{
if (Equals(value, _routelabels)) return;
_routelabels = value;
OnPropertyChanged(nameof(routeLabels));
}
}
Run Code Online (Sandbox Code Playgroud)
在 Xaml 中设置绑定时,此绑定工作正常。问题不是可观察的集合问题是我不知道如何在后面的代码中设置绑定。
概括:
我需要知道如何做到这一点(itemsource 绑定):
<ListView x:Name="listviewofroutes" ItemsSource="{Binding routeLabels}">
</ListView>
Run Code Online (Sandbox Code Playgroud)
在后面的代码中。
任何帮助,将不胜感激。
要以编程方式在控件上设置绑定,您必须在扩展方法中将其作为参数传递。参考链接:扩展方法,或者成员方法
例如,尝试:
listviewofroutes.SetBinding(ListView.ItemsSourceProperty, "routeLabels")
//Or,
listviewofroutes.SetBinding(ListView.ItemsSourceProperty, new Binding("routeLabels"))
Run Code Online (Sandbox Code Playgroud)
这将路径之间的绑定设置为“routeLabels”和BindingContext
作为视图模型的控件。
此外,建议根据 C# 属性的标准命名策略将“routeLabels”更改为“RouteLabels”。
归档时间: |
|
查看次数: |
4560 次 |
最近记录: |