Data binding windows phone中列表框中的数据绑定/填充项

Data binding windows phone中列表框中的数据绑定/填充项,data-binding,listbox,windows-phone,Data Binding,Listbox,Windows Phone,我有两个不同的课程,即: public class Floor { private string fname; public Floor(string name) { fname = name; } public int FName { set { fname = value; } get { return fname; } } } public class Building { List&l

我有两个不同的课程,即:

    public class Floor { 
     private string fname;
   public Floor(string name)
   {
     fname = name;
   }

   public int FName
   {
      set { fname = value; }
      get { return fname; }
   }

}

public class Building 
{
   List<Floor> floors;
   string _bName;

   public Building(string bname)
   {

       _bName = bname;

      floors = new List<Floors>();

      for(int i = 0; i < 3; i++)
      {
           floors.Add(new Floor("floor" + (i + 1)));
      }
   }

   public string BName
   {
      set{ _bName = value; }
      get{ return _bName; }
   }

   public List<Floor> Floors
   {
      set { floors = value; }
      get { return floors; }
   }

}
公共课堂{
私有字符串fname;
公共楼层(字符串名称)
{
fname=名称;
}
公共int FName
{
设置{fname=value;}
获取{return fname;}
}
}
公共班级大楼
{
列出楼层;
字符串名称;
公共建筑(字符串bname)
{
_bName=bName;
楼层=新列表();
对于(int i=0;i<3;i++)
{
增加(新楼层(“楼层”+(i+1));
}
}
公共字符串BName
{
设置{u bName=value;}
获取{return\u bName;}
}
公共楼层列表
{
设置{floors=value;}
获取{返回楼层;}
}
}
在我的XAML(MainPage.XAML)中:


在我的XAML.cs(MainPage.XAML.cs)中

observedcollectionbuildings=新的observedcollection();
对于(int i=0;i<2;i++)
{
增加(新建筑物(“建筑物”+(i+1));
}
lstBuilding.ItemsSource=建筑物
问题是:

如何使用XAML访问Floor类内部的FName? 我所做的是:

<TextBlock Text="{Binding Path=Floors.FName }" />  

但它不起作用(


很抱歉发了这么长的帖子。

您的代码本身有缺陷,因为您试图访问楼层,而楼层又是一个集合/列表,当您有
时,不清楚您指的是哪一层楼,或者您想做什么

如果您只想参考一楼,您可以尝试

但是,如果您试图访问每栋建筑中每一层的数据,您需要更改xaml以使其正常工作,这称为嵌套绑定

 <ListBox x:Name="listBuilding">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <ListBox ItemsSource="{Binding Floors}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=FName}"></TextBlock>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>

<TextBlock Text="{Binding Path=Floors.FName }" />  
 <ListBox x:Name="listBuilding">
                <ListBox.ItemTemplate>
                    <DataTemplate>
                        <ListBox ItemsSource="{Binding Floors}">
                            <ListBox.ItemTemplate>
                                <DataTemplate>
                                    <TextBlock Text="{Binding Path=FName}"></TextBlock>
                                </DataTemplate>
                            </ListBox.ItemTemplate>
                        </ListBox>
                    </DataTemplate>
                </ListBox.ItemTemplate>
            </ListBox>