【问题标题】:ListView Data template binding in Xamarin formsXamarin 表单中的 ListView 数据模板绑定
【发布时间】:2019-03-14 18:20:00
【问题描述】:

我一直在尝试在列表视图中填充数据模板,但它没有显示任何内容。我的代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<ContentPage xmlns="http://xamarin.com/schemas/2014/forms" 
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" 
         x:Class="BlueDemo.ScoreDisplay">
<ContentPage.Content>                 
       <StackLayout> 
        <ListView x:Name="listView" Grid.ColumnSpan="2">
           <ListView.ItemTemplate>
           <DataTemplate>
              <ViewCell>
                <Grid>
                  <Grid.ColumnDefinitions>
                     <ColumnDefinition Width="0.7*"/> 
                     <ColumnDefinition Width="0.3*"/> 
                  </Grid.ColumnDefinitions>     
                  <Label Text="{Binding name}" FontAttributes="Bold"/>
                  <Label Text="{Binding score}" Grid.Column ="1"/>                
                </Grid>
              </ViewCell>
          </DataTemplate>
          </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
</ContentPage.Content>    

public partial class ScoreDisplay : ContentPage
{
    public static List<int> Score = new List<int>();
    public static List<string> Name = new List<string>();
     public string[] nme = new string[10];

    public static int[] scr = new int[10];

    public ObservableCollection<Data> mySource{get;set;}
    public ScoreDisplay()
    {
        InitializeComponent();
        BindingContext = this;


        nme = Name.ToArray();
        scr=Score.ToArray();

        mySource = new ObservableCollection<Data>();
        for(int i=0;i<nScore.Count;i++)
        {
            mySource.Add(nnew Data {name = nme[i], score = 
            scr[i].ToString()});
        }
        listView.ItemsSource = mySource;
    }  
}

public class Data
{
   public string name { get; set; }
   public string score { get; set; } 
}

我有两个列表(姓名和分数)从其他页面获取值。我不确定如何将其中的每个值与列表视图的名称和分数绑定。 编辑:工作代码

【问题讨论】:

  • 嘿,有用吗?
  • 我还没试过。别的东西出现了。完成后会尽快通知您。

标签: listview xamarin xamarin.forms datatemplate


【解决方案1】:

原因:

Label.Text 是一个字符串。但是在您的类Data 中,属性名称是List

解决方案:

参考以下代码。

public class Data
{
    public string name { get; set; }
    public string score { get; set; }
}

public ObservableCollection<Data> mySource { get; set; }

List<int> ScoreList = new List<int>();
List<string> NameList = new List<string>();    

public ScoreDisplay ()
{
  InitializeComponent();

  BindingContext = this;

  NameList.Add("jack");
   // or you can set the list with  the other part of the app

  ScoreList.Add(60);
   //...

  mySource = new ObservableCollection<Data>();

  for (int i=0;i<ScoreList.Count;i++)
   {
      mySource.Add(new Data { name = NameList[i], score = ScoreList[i].ToString()});
   }


  listView.ItemsSource = mySource;
}

【讨论】:

    【解决方案2】:

    您错过了在 ListView xaml 中绑定的 ItemSource。添加这一行而不是

    <ListView x:Name="listView" Grid.ColumnSpan="2">
    

    <ListView x:Name="listView" ItemsSource="{Binding DataDisplay}" Grid.ColumnSpan="2">
    

    【讨论】:

    • 嗨@Prashanth,它在cs部分。
    • 但您还需要在 Xaml 中进行初始化。它是一个在键值下绑定属性的集合。试试这个
    • 检查您的姓名和分数在DataDisplay中是否有值或为空
    • 它在我通过数组初始化名称和分数时起作用。将列表更改为 Array 然后 new Data{ name= Name[0], score = Score[0]} 但这不是我想要遵循的方法,因为列表的数量总是会改变。
    • 从哪里将数据传递给名称。您使用 ObservableCollection. 创建
    【解决方案3】:
    public partial class MainPage : ContentPage
    {
        List<ScoreModel> ScoreList = new List<ScoreModel>();
        List<NameModel> NameList = new List<NameModel>();
        public MainPage()
        {
            InitializeComponent();
    
            NameList.Add(new NameModel { Name = "Name1", Id = 0 });
            NameList.Add(new NameModel { Name = "Name2", Id = 1 });
    
            ScoreList.Add(new ScoreModel { Id = 0, Score = 1 });
            ScoreList.Add(new ScoreModel { Id = 1, Score = 2 });
    
            var DataDisplay = new List<Data>();
    
            foreach (var item in NameList)
            {
                foreach (var item1 in ScoreList)
                {
                    if(item.Id==item1.Id)
                    {
                        DataDisplay.Add(new Data { name = item.Name, score = item1.Score });
                    }
                }
            }
            listView.ItemsSource = DataDisplay;
        }
    }
    public class Data
    {
        public string name { get; set; }
        public int score { get; set; }
    }
    public class NameModel
    {
        public int Id { get; set; }
        public string Name { get; set; }
    }
    public class ScoreModel
    {
        public int Id { get; set; }
        public int Score  { get; set; }
    }
    

    你的xml:

    <StackLayout>
        <ListView x:Name="listView">
            <ListView.ItemTemplate>
                <DataTemplate>
                    <ViewCell>
                        <Grid>
                            <Grid.ColumnDefinitions>
                                <ColumnDefinition Width="0.7*" />
                                <ColumnDefinition Width="0.3*" />
                            </Grid.ColumnDefinitions>
    
                            <Label Text="{Binding name}" Grid.Column="0"
                                   FontAttributes="Bold" />
                            <Label Text="{Binding score}"
                                   Grid.Column="1" />
                        </Grid>
                    </ViewCell>
                </DataTemplate>
            </ListView.ItemTemplate>
        </ListView>
    </StackLayout>
    

    希望这可以解决您的问题。

    【讨论】:

      猜你喜欢
      • 2016-09-08
      • 1970-01-01
      • 2016-10-10
      • 2011-04-30
      • 1970-01-01
      • 1970-01-01
      • 2018-10-19
      • 2018-08-14
      • 2016-06-26
      相关资源
      最近更新 更多