Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/297.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在没有XAML的ListView数据模板中设置从ItemsSource到Label的绑定_C#_Xamarin_Binding - Fatal编程技术网

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 };
   })
};