2012-08-22 22 views
0

我有兩個獨立的類即:數據綁定/填充在Windows手機在列表框中的項目

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

} 
在我的XAML

(MainPage.xaml中):

<ListBox x:Name="lstBuilding" Background="White" Foreground="Black"> 
    <ListBox.ItemTemplate> 
    <DataTemplate> 
     <StackPanel Orientation="Horizontal" Margin="10,0,0,15">     
      <StackPanel> 
      <TextBlock Text="{Binding Path=BName }" />                      
      </StackPanel> 
     </StackPanel> 
    </DataTemplate> 
    </ListBox.ItemTemplate> 
</ListBox> 

,並在我的XAML.cs( MainPage.xaml.cs中)

ObservableCollection<Building> buildings = new ObservableCollection< Building>(); 

for(int i = 0; i < 2; i++) 
{ 
    buildings.Add(new Building("building" + (i + 1))); 
} 

lstBuilding.ItemsSource = buildings 

這裏的問題:

如何使用XAML訪問樓層類中的FName? 我做的是:

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

但它沒有工作。 :(

很抱歉的長期職位。

回答

2

你的代碼本身是有缺陷的,因爲你試圖訪問地板這又是一個集合/列表,當你有<TextBlock Text="{Binding Path=Floors.FName }" />,目前尚不清楚你指的是哪個樓或者你正在做什麼?

如果你想參考下到一樓只有你可以嘗試<TextBlock Text="{Binding Path=Floors[0].FName }" />

但櫃面你試圖訪問在每棟樓的每個樓層的數據,您需要更改XAML爲了工作,它被稱爲嵌套綁定(Nested binding)。

<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> 
+0

感謝這個...我現在明白如何綁定的作品! – xiriusly

+0

謝謝,請投票,如果它幫助你:) –

相關問題