Reputation: 713
i have two separate classes namely:
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; }
}
}
in my 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>
and in my 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
Here's the question:
how can i access FName inside Floor class using XAML? What i did is :
<TextBlock Text="{Binding Path=Floors.FName }" />
but it didn't work. :(
Sorry for the long post.
Upvotes: 0
Views: 3942
Reputation: 2061
Your code itself is flawed because you are trying to access Floors which is again a Collection/List, When you have <TextBlock Text="{Binding Path=Floors.FName }" />
, it is not clear which floor are you referring to or what you are trying to do?
If you want reference to the first floor only you can try <TextBlock Text="{Binding Path=Floors[0].FName }" />
But incase you are trying to access data of each floor in each building you need to change the xaml for that to work. Its called 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>
Upvotes: 2