xamarin - 如何为 Xamarin 中的绑定设置空路径?
问题描述
我有一个绑定,我不需要路径。如果我在 xaml 中设置 ItemTemplate 效果很好。
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage
x:Class="TestBindingPath.MainPage"
xmlns="http://xamarin.com/schemas/2014/forms"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml">
<ListView x:Name="listview1" ItemsSource="{Binding Students}">
<ListView.ItemTemplate>
<DataTemplate>
<ViewCell>
<Label Text="{Binding}" />
</ViewCell>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</ContentPage>
但是如果我在代码中设置 ItemTemplate 它会引发异常
listview1.ItemTemplate = new DataTemplate(() => {
Label label = new Label();
label.SetBinding(Label.TextProperty, new Binding());
return (new ViewCell() {
View = label
});
});
那么如何为代码中的绑定设置一个空路径?
解决方案
这是绑定方法结构。
public Binding(string path, BindingMode mode = BindingMode.Default, IValueConverter converter = null, object converterParameter = null, string stringFormat = null, object source = null);
尝试这样做。
listview1.ItemTemplate = new DataTemplate(() => {
Label label = new Label();
label.SetBinding(Label.TextProperty, new Binding(source:Students));
return (new ViewCell() {
View = label
});
});
学生属性必须在代码 bihind cs 中
推荐阅读
- .net-core - 如何在不访问 nuget.org 的情况下构建/部署独立的 .netcore 应用程序
- android - 安卓相机X。拍摄开始后如何更改相机参数?
- mysql - #1136 - 列计数与第 1 行的值计数不匹配错误
- r - 是否可以 dplyr::lag 上一年而不是上一行?
- python-3.x - 如何按 Pandas 的特定给定值分组?
- css - 如何将边框颜色作为线性渐变放在表格行的边框上?
- python - 在运行时创建类是什么意思?还有运行时在python中意味着什么?
- python - Pandas pivot_table 和额外的索引级别
- javascript - 布尔和条件语句如何在事件侦听器中执行?
- python - 无法理解 Python 负字符串切片