C# 在没有XAML的ListView数据模板中设置从ItemsSource到Label的绑定
我已经为我的C# 在没有XAML的ListView数据模板中设置从ItemsSource到Label的绑定,c#,xamarin,binding,C#,Xamarin,Binding,我已经为我的ListView创建了DataTemplate,但是没有显示ListView项目的文本。如何从项目资源绑定文本 List<string> stringList; ListView myListView = new ListView { ItemsSource = stringList, ItemTemplate = new DataTemplate(() => { StackLayout sl = new StackLa
ListView
创建了DataTemplate
,但是没有显示ListView项目的文本。如何从项目资源
绑定文本
List<string> stringList;
ListView myListView = new ListView
{
ItemsSource = stringList,
ItemTemplate = new DataTemplate(() =>
{
StackLayout sl = new StackLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
Orientation = StackOrientation.Horizontal,
};
sl.Children.Add(new Label
{
FontSize = 14,
});
return new ViewCell { View = sl };
})
List-stringList;
ListView myListView=新建ListView
{
ItemsSource=stringList,
ItemTemplate=新数据模板(()=>
{
StackLayout sl=新的StackLayout
{
VerticalOptions=LayoutOptions.FillAndExpand,
方向=堆叠方向。水平,
};
sl.Children.Add(新标签
{
FontSize=14,
});
返回新的ViewCell{View=sl};
})
在添加到StackLayout之前初始化标签,例如,将其称为temp
,并将其像这样绑定到自定义对象:
temp.SetBinding (Label.TextProperty, "TemplatePropertyThatYouWish");
这是一个字符串列表:
temp.SetBinding(Label.TextProperty, ".");
然后将其添加到StackLayout中
代码:
List-stringList=新列表{“a”、“b”、“c”};
ListView myListView=新建ListView
{
ItemsSource=stringList,
ItemTemplate=新数据模板(()=>
{
StackLayout sl=新的StackLayout
{
VerticalOptions=LayoutOptions.FillAndExpand,
方向=堆叠方向。水平,
};
var temp=新标签
{
FontSize=14,
};
临时设置绑定(Label.TextProperty,“.”;
sl.Children.Add(临时);
返回新的ViewCell{View=sl};
})
};
但是如果我需要用字符串列表绑定ItemSource,我必须使用第二个参数是什么呢?更新@InfernumDeus
List<string> stringList = new List<string> { "a", "b", "c" };
ListView myListView = new ListView
{
ItemsSource = stringList,
ItemTemplate = new DataTemplate(() =>
{
StackLayout sl = new StackLayout
{
VerticalOptions = LayoutOptions.FillAndExpand,
Orientation = StackOrientation.Horizontal,
};
var temp = new Label
{
FontSize = 14,
};
temp.SetBinding(Label.TextProperty, ".");
sl.Children.Add(temp);
return new ViewCell { View = sl };
})
};